SHARE
TWEET

index.php

a guest Mar 19th, 2017 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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="submit" value="Register">
  29.         </form>
  30.  
  31.         <a href="login.php">Already have an account?</a>
  32.  
  33.         <?php
  34.             if(isset($_POST['submit'])){
  35.               //database
  36.               include("../secure/database.php");
  37.               //connecting to server
  38.               $link=mysqli_connect(HOST,USERNAME,PASSWORD,DBNAME) or die("Connect Error ".mysqli_error($link));
  39.              
  40.  
  41.               checkUser($link, htmlspecialchars($_POST['username']));
  42.             }
  43.  
  44.             function checkUser($link, $username){
  45.               //create a prepare statement not working
  46.               $query = "SELECT * FROM login WHERE user_name=?";
  47.               $stmt = $link->stmt_init();
  48.               // failsafe for sql query
  49.               $stmt=$link->prepare($query) or die("Prepare error: ".mysqli_error($link));
  50.  
  51.               //Bind the $username to the ? to look for the username in the table
  52.               $stmt->bind_param("s", $username) or die("Bind error".mysqli_error($link));
  53.  
  54.               //Runs the statement
  55.               $stmt->execute();
  56.  
  57.               //returns the 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 '<br>Username taken.<br>';
  63.               }
  64.               else{
  65.                   addUsers($link,$username,htmlspecialchars($_POST['password']));
  66.                 }
  67.               $stmt->close();
  68.             }
  69.  
  70.  
  71.             function addUsers($link,$username,$password){
  72.               //hash the password
  73.               $password=password_hash($password,PASSWORD_DEFAULT);
  74.  
  75.               $stmt = $link->stmt_init();
  76.               //insert value into the table Not working
  77.               $stmt = $link->prepare("INSERT INTO login (user_name,password) VALUES (?,?)")  or die("Prepare addUsers error: ".mysqli_error($link));;
  78.  
  79.               //bind the $username and $password to the ?
  80.               $stmt->bind_param("ss",$username, $password) or die("Bind addUsers error: ".mysqli_error($link));;
  81.  
  82.               //run the statement
  83.               $stmt->execute();
  84.  
  85.               $stmt->close();
  86.  
  87.               echo "<br>user added<br>";
  88.               //Session for login
  89.               $_SESSION['login_user']=$username;
  90.               //head to profile if login has a username
  91.               echo "<a href='profile.php'>login</a>";
  92.               mysqli_close($link);
  93.               header('profile.php');
  94.             }
  95.  
  96.           ?>
  97.     </body>
  98.  
  99. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top