Web Analytics Made Easy -
StatCounter Cannot log in what's wrong with my code? - CodingForum

Announcement

Collapse
No announcement yet.

Cannot log in what's wrong with my code?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot log in what's wrong with my code?

    register.php

    Code:
    <?php 
    
    require('config.php');
    $error="";
    
    if (isset($_POST['submit'])=="Sign up") {
        if(!$_POST['name'])  $error.="<br/>Please enter your name";
        if (!$_POST['email']) $error.="<br/>Please enter your email";
            else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.="<br/>Please enter a valid email address" ;
        if (!$_POST['password']) $error.="<br/>Please enter your password";
            else{
                if(strlen($_POST['password'])<8) $error.= "<br/>Please enter a password with atleast 8 charachters";
                if (!preg_match('`[A-Z]`', $_POST["password"])) $error.="<br/>Please include atleast one capital letter in your password";
                }
        if ($error) echo "There were error(s) in your signup details:" .$error;
        else{
            $query= "SELECT * FROM `members` WHERE email='".mysqli_real_escape_string($link,$_POST['email'])."'";
            $match= mysqli_query($link,$query); 
            $results= mysqli_num_rows($match);
            if ($results) echo "That email address is already registered. Do you want to log in ?";
            else {
           $name = mysqli_real_escape_string($link,$_POST['name']);
           $email = mysqli_real_escape_string($link,$_POST['email']);
           $password = password_hash($_POST['password'],PASSWORD_DEFAULT);
     
            $query= "INSERT INTO `members`(name,email,password) VALUES('$name','$email','".$password."')";
            $result = mysqli_query($link,$query);
                  if($result){
             echo "<div class='form'>
    <h3>You are registered successfully.</h3>
    <br/>Click here to <a href='login.php'>Login</a></div>";
            }
                
            
                         
            }
        }
    }
    
    else{
    
    ?>
    
    <form method="post">
        <h1>Registration</h1><br>
        <label for="name">Name</label>
        <input type="text" name="name" /><br>
        <label for="email">Email</label>
        <input type="email" name="email" id="email" /><br>
        <label for="password">Password</label>
        <input type="password" name="password" /><br>
        <input type="submit" name="submit" value="Sign up" /><br>
         
    
    </form>
    
    <?php } ?>

    login.php

    Code:
    <?php 
    
    
    require("config.php");
    
    session_start();
    
    if (isset($_POST['submit'])=="Login"){
           $name = mysqli_real_escape_string($link,$_POST['name']);
           $password = password_hash($_POST['password'],PASSWORD_DEFAULT);
     
        $query= "SELECT * FROM `members` WHERE name='$name' AND password='".$password."'";
        
        $result= mysqli_query($link,$query)  or die(mysql_error());
        $rows = mysqli_num_rows($result);
             if ($row = mysqli_fetch_array($result)) {
            $_SESSION['id'] = $row['id'];
            $_SESSION['name'] = $row['name'];
                
    	    header("Location: profile.php");
                 }else{
    	echo "<div class='form'>
    <h3>Username/password is incorrect.</h3>
    <br/>Click here to <a href='login.php'>Login</a></div>";
    	}
     
        
    }
    else{
    ?>
    <form method="post">
        <label for="name">Name</label>
        <input type="name" name="name" /><br>
        <label for="password">Password</label>
        <input type="password" name="password" /><br>
        <input type="submit" name="submit" value="Login" />
    </form>
    <p>Not registered yet? <a href='register.php'>Register Here</a></p>
    <?php } ?>
    Last edited by vinyl-junkie; Sep 29, 2016, 01:25 AM. Reason: added code tags

  • #2
    password_hash() is used when first entering the password, during registration. password_verify() is used to test if the submitted password matches the original. There are examples how to use these two functions in the php.net documentation.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment

    Working...
    X