Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- root@dev:/var/www/html/php# cat server.php
- <?php
- ini_set('display_errors',1); error_reporting(E_ALL);
- $host = "host=localhost";
- $dbname = "dbname=server";
- $creds = "user=postgres password=postgres";
- // connect to the database
- $db = pg_connect("$host $dbname $creds");
- // if the register button is clicked
- if(isset($_POST['register'])) {
- $username = $_POST['username'];
- $password1 = $_POST['password'];
- $password2 = $_POST['password_2'];
- // ensure that form fields are filled properly
- if (empty($username)) {
- array_push($errors, "Username is required.");
- }
- if (empty($password_1)) {
- array_push($errors, "Passphrase is required.");
- }
- if ($password_1 != $password_2) {
- array_push($errors, "The two passwords don't match.");
- }
- }
- // if there are no errors, save user to database
- if (count($errors) == 0) {
- $password = md5($password_1); // encrypt password before storing
- $sql = <<<EOF
- INSERT INTO username (username, password)
- VALUES ('$username','$password');
- EOF;
- $ret = pg_query($db, $sql);
- if(!$ret) :
- echo pg_last_error($db);
- else :
- exit("Table created successfully!".PHP_EOL);
- endif;
- ?>
- =================================================================
- root@dev:/var/www/html/php# cat index.php
- <html>
- <body>
- <form action="register.php" method="post">
- Username: <input type="text" name="username"><br>
- Password: <input type="password" name="password"><br>
- Confirm Password: <input type="password" name="password_2"><br>
- <input type="submit" >
- </form>
- </body>
- </html>
- <?php
- require('server.php');
- ?>
- ======================================================================
- root@dev:/var/www/html/php# cat register.php
- <?php
- echo "<h1> YOU ENTERED THE FOLLOWING </h1><br>";
- echo "username: ".$_POST["$username"]."<br>";
- echo "username: ".$_POST["$password"]."<br>";
- ?>
- <html>
- <h1> Thanks for registering! </h1>
- </html>
- <?php require("server.php");?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement