how to automatically login after registration in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • impin
    New Member
    • Jul 2010
    • 127

    how to automatically login after registration in php?

    how to automatically login after the user successfully completes registration.

    now, the user after registration, need to enter username and password again to login. i don't want to enter username and password, he automatically loggged in after registration.

    login.php

    Code:
    <?php
    include("config.php");
    session_start();
    
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
    	// username and password sent from form 
    
    /*$myusername= ucfirst ($_POST['uname']); 
    $mypass=($_POST ['password']); 
    $mypassword=md5($mypass);*/
    $email = $_POST['email'];
    $password = $_POST['password'];
    
    $sql="SELECT * FROM member WHERE email='$email' and password='$password'";
    $result=mysql_query($sql);
    /*$row=mysql_fetch_array($result);
    $active=$row['active'];*/
    
    $count=mysql_num_rows($result);
    
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1 and $email=="admin@reachmena.com")
    {
    session_register("admin");
    $_SESSION['login_user']=$email;
    
    header("location: admin.php");
    }
    else if($count==1)
    {
    session_register("email");
    $_SESSION['login_user']=$email;
    
    header("location: welcome_test.php");
    }
    else 
    {
    $error="Your Login Name or Password is invalid";
    }
    }
    ?>
    lock.php

    Code:
    <?php
    include('config.php');
    session_start();
    $user_check=$_SESSION['login_user'];
    
    $ses_sql=mysql_query("select mname from member where email='$user_check' ");
    
    $row=mysql_fetch_array($ses_sql);
    
    $login_session=$row['mname'];
    
    if(!isset($login_session))
    {
    header("Location:home.php");
    }
    ?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Based on that code, whether or not the user is logged in is determined by checking if the $_SESSION['login_user'] element has a valid user email. So if you want your registration script to log the user in, put the email into that session variable.


    On another note, the session_registe r() function has been obsolete for a long time, and has been removed from PHP altogether as of PHP 5.4. Do not use it! - See the manual entry on sessions for details on how to properly use sessions in modern PHP code.

    Comment

    Working...