How to auto-login to index page after registration without going to login page ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yateesh
    New Member
    • Dec 2012
    • 36

    #1

    How to auto-login to index page after registration without going to login page ?

    I want users to be redirected to home page after registration without logging in again .
    I searched , but didn't find a solution .
    So do tell what can be done .
    Thank you .


    Here is my register.php
    Code:
    <?php
      
        require("common.php");
            
        if(!empty($_POST))
        {      
            if(empty($_POST['username']))
            {           
                die("Please enter a username.");
            }        
            
            if(empty($_POST['password']))
            {
                die("Please enter a password.");
            }        
           
            if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
            {
                die("Invalid E-Mail Address");
            }
          
            $query = "SELECT 1 FROM users WHERE username = :username ";
     
            $query_params = array( ':username' => $_POST['username'] );
            
            try
            {         
                $stmt = $db->prepare($query);
                $result = $stmt->execute($query_params);
            }
            catch(PDOException $ex)
            {          
                die("Failed to run query: " . $ex->getMessage());
            }
         
            $row = $stmt->fetch();
          
            if($row)
            {
                die("This username exists . Try another .");
            }
           
            $query = " SELECT 1 FROM users WHERE email = :email ";
            
            $query_params = array( ':email' => $_POST['email'] );
            
            try
            {
                $stmt = $db->prepare($query);
                $result = $stmt->execute($query_params);
            }
            catch(PDOException $ex)
            {
                die("Failed to run query: " . $ex->getMessage());
            }
            
            $row = $stmt->fetch();
            
            if($row)
            {
                die("This email is already registered");
            }
                 
            $query = "
                INSERT INTO users (
                    username,
                    password,
                    salt,
                    email
                ) VALUES (
                    :username,
                    :password,
                    :salt,
                    :email
                )
            ";
                   
            $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
                    
            $password = hash('sha256', $_POST['password'] . $salt);
                  
            for($round = 0; $round < 65536; $round++)
            {
                $password = hash('sha256', $password . $salt);
            }
           
            $query_params = array(
                ':username' => $_POST['username'],
                ':password' => $password,
                ':salt' => $salt,
                ':email' => $_POST['email']
            );
            
            try
            {        
                $stmt = $db->prepare($query);
                $result = $stmt->execute($query_params);
            }
            catch(PDOException $ex)
            {            
                die("Failed to run query: " . $ex->getMessage());
            }
            
            header("Location: login.php");
    
        }
        
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    two possibilities I know of:
    - using OpenID (haven’t used it myself yet)
    - providing some kind of secondary login credentials via cookies

    some notes to your PHP:
    - for using Exceptions with PDO you need to enable Exception handling explicitly
    - you only need one try...catch block over all your PDO code
    - a salt can be very conveniently applied through hash_hmac()
    - there is no need for the redirect header, you can just include login.php directly

    Comment

    Working...