Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- if(isset($_SESSION['login_user'])){
- header("location: profile.php");
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Register</title>
- <style>
- body {
- color: #999999;
- font: 14px/1.5em "Lucida Grande",Arial,sans-serif;
- margin: 20px;
- width: auto;
- }
- </style>
- </head>
- <body>
- <form method="POST" action="index.php">
- <label>Username:</label>
- <input type="text" name="username" id="username" placeholder="username">
- <br>
- <label>Password:</label>
- <input type="password" name="password" id="password" placeholder="******">
- <br>
- <input type="submit" name="register" value="Register">
- </form>
- <a href="login.php">Already have an account?</a>
- <?php
- if(isset($_POST['register'])){
- //database
- include("../secure/database.php");
- //connecting to server
- $link=new mysqli(HOST,USERNAME,PASSWORD,DBNAME) or die("Connect Error ".mysqli_error($link));
- $username=htmlspecialchars($_POST['username']);
- $password=htmlspecialchars($_POST['password']);
- echo "pass the isset";
- checkUser($link,$username);
- }
- function checkUser($link, $username){
- //create a prepare statement not working
- $query="SELECT * FROM login WHERE username=?";
- $stmt = $link->stmt_init();
- if(!$stmt=$link->prepare($query)){
- echo "prepare not working";
- }
- else{
- //bind parameter
- $stmt->bind_param("s", $username);
- //excute it
- $stmt->execute();
- //get result
- $result = $stmt->get_result();
- //return the num of row. If 0 then username is free else taken
- if($result->num_rows!=0){
- echo 'Username taken.';
- }
- else{
- echo 'Username available';
- addUsers($link,$username,$password);
- }
- echo "check user works";
- $stmt->close();
- }
- }
- function addUsers($link,$username,$password){
- //hash the password
- $password=password_hash($password,PASSWORD_DEFAULT);
- $stmt = $link->stmt_init();
- //insert value into the table Not working
- $stmt = $link->prepare("INSERT INTO login (username,password) VALUES (?,?)");
- $stmt->bind_param("ss",$username, $password);
- $stmt->execute();
- $stmt->close();
- echo "add user works";
- //Session for login
- $_SESSION['login_user']=$username;
- //head to profile if login has a username
- header('profile.php');
- mysqli_close($link);
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement