creating login session

Discussion in 'PHP' started by newbie12345, May 8, 2010.

  1. #1
    after checking if the login was successful i want to create a session to check to see if a user is logged in or not
    can any one help me with that
    plz


    
    session_start();
    include ('connect.php');
    
          $user  = $_POST['u_name'];
          $pass   = $_POST['pwd'];
    	  
    if(!$user){
    echo "please enter your username. <br>";
    }
    if(!$pass){
    	echo "please enter your password";
    	}
    if ($user && $pass){
    	   $sql= ("SELECT * FROM user_accounts WHERE username = '$user' AND password = '$pass'");
    	   $res = mysql_query($sql);
    	   $count = mysql_num_rows($res);
    	   
    	  if ($count ==0){
    	     echo "ur user name or password may be incorrect! try again";
    	  } ELSE {
    	  if ($count ==1){
    	   $row = mysql_fetch_array($res);
    	   echo "u have been logged in successfully";
    
    	   }
    	  }
    
    }
    
    PHP:
     
    newbie12345, May 8, 2010 IP
  2. Lam3r

    Lam3r Active Member

    Messages:
    235
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You need to set a session variable when they login, and check it every time they load another page that they need to be logged in to see. In the success statement add something like this:
    
    $_SESSION['loggedin'] = 1;
    
    PHP:
    Then on any page they need to be logged in add this at the top:
    
    if(!isset($_SESSION['loggedin']){   // if they haven't logged in and set the $_SESSION variable
             header("Location: http://www.yoursite.com/login.php");   // redirect them to login.php
    }
    
    PHP:
    Let me know how that works for ya!
     
    Lam3r, May 8, 2010 IP