Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.69 KB | None | 0 0
  1. <?php
  2.     session_start();
  3.         if(isset($_SESSION['login_user'])){
  4.         header("location: profile.php");
  5.     }
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9.     <head>
  10.         <title>Register</title>
  11.         <style>
  12.             body {
  13.                 color: #999999;
  14.                 font: 14px/1.5em "Lucida Grande",Arial,sans-serif;
  15.                 margin: 20px;
  16.                 width: auto;
  17.             }
  18.         </style>
  19.     </head>
  20.     <body>
  21.         <form method="POST" action="index.php">
  22.             <label>Username:</label>
  23.             <input type="text" name="username" id="username" placeholder="username">
  24.             <br>
  25.             <label>Password:</label>
  26.             <input type="password" name="password" id="password" placeholder="******">
  27.             <br>
  28.             <input type="submit" name="register" value="Register">
  29.         </form>
  30.        
  31.         <a href="login.php">Already have an account?</a>
  32.         <?php
  33.             if(isset($_POST['register'])){
  34.                     //database
  35.                     include("../secure/database.php");
  36.                     //connecting to server
  37.                     $link=new mysqli(HOST,USERNAME,PASSWORD,DBNAME) or die("Connect Error ".mysqli_error($link));
  38.                     $username=htmlspecialchars($_POST['username']);
  39.                     $password=htmlspecialchars($_POST['password']);
  40.                     echo "pass the isset";
  41.                     checkUser($link,$username);
  42.             }
  43.             function checkUser($link, $username){
  44.                     //create a prepare statement not working
  45.                         $query="SELECT * FROM login WHERE username=?";
  46.                         $stmt = $link->stmt_init();
  47.                         if(!$stmt=$link->prepare($query)){
  48.                             echo "prepare not working";
  49.                         }
  50.                         else{
  51.                             //bind parameter
  52.                             $stmt->bind_param("s", $username);
  53.  
  54.                             //excute it
  55.                             $stmt->execute();
  56.  
  57.                             //get result
  58.                             $result = $stmt->get_result();
  59.  
  60.                             //return the num of row. If 0 then username is free else taken
  61.                             if($result->num_rows!=0){
  62.                                 echo 'Username taken.';
  63.                             }
  64.                             else{
  65.                                 echo 'Username available';
  66.                                 addUsers($link,$username,$password);
  67.                             }
  68.                             echo "check user works";
  69.                             $stmt->close();
  70.                         }
  71.                     }
  72.                     function addUsers($link,$username,$password){
  73.                         //hash the password
  74.                         $password=password_hash($password,PASSWORD_DEFAULT);
  75.  
  76.                         $stmt = $link->stmt_init();
  77.                         //insert value into the table Not working
  78.                         $stmt = $link->prepare("INSERT INTO login (username,password) VALUES (?,?)");
  79.  
  80.                             $stmt->bind_param("ss",$username, $password);
  81.  
  82.                             $stmt->execute();
  83.                            
  84.                             $stmt->close();
  85.                             echo "add user works";
  86.                         //Session for login
  87.                         $_SESSION['login_user']=$username;
  88.                         //head to profile if login has a username
  89.                         header('profile.php');
  90.                         mysqli_close($link);
  91.                     }
  92.  
  93.     ?>
  94.     </body>
  95.  
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement