How to get session info from the login page after logging in?

时间:2018-04-18 18:02:09

标签: php

I made a login.php page, but now I made an index.php page with content on it. I sucessfully finished the login.php page so that the login works. I have a couple of usernames and passwords in my database. Now the issue is, how do I get the session info from the login.php page to show on the index.php page that I am logged in? I would like to print out that I am logged in as a particular user. I also had the session.start(); at the top of my login.php code.

Here is my index.php php code:

  <?php
                session_start();
                if(!session_is_registered(`name`))
                {
                  header("location: login.php");
                }
              $user = session_is_registered(`name`);
                $pass = session_is_registered(`pwd`);

              echo "<h3>" . $user . "</h3>";
          ?> 

2 个答案:

答案 0 :(得分:0)

I would use isset and empty: example:

session_start();
$_SESSION['blah']= "blah blah";
if(isset($_SESSION['blah']) && !empty($_SESSION['blah'])) {
   echo 'Set and not empty, and no undefined index error!';
}

array_key_exists is a nice alternative to using isset to check for keys:

session_start();
if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])) {
    echo 'Set and not empty, and no undefined index error!';
}

after login you need set value on session for later validate if exist !!

答案 1 :(得分:0)

this should be your login.php
   <?php
  session_start();
  if(isset($_POST['submit'])){
  include 'you database connectivity file';

  $uname=mysqli_real_escape_string($conn, $_POST['uname']);
  $pass=mysqli_real_escape_string($conn, $_POST['pass']);
  if((empty($uname)) || (empty($pass))){
  header("Location: ../login.php?login=erroremptyform");
  exit();
 }else{
$sql = "select * from table_name where user_name='$uname'";
$result=mysqli_query($conn, $sql);
$resultCheck=mysqli_num_rows($result);
if($resultCheck<1){
  header("Location: ../login.php?login=errornorec");
  exit();
}else{
  if($row = mysqli_fetch_assoc($result)){
  if(base64_encode($pass) == $row['password']){
    //login in the user here
    $array = array(
        'uname' => $row['user_name'],
        'fname' => $row['f_name'],
        'lname' => $row['l_name'],
        'email' => $row['email'],
        'contact' => $row['contact_number'],
        'addrs' => $row['address'],
        'id' => $row['id'],
     );

     $_SESSION['logindetails'] = $array;
    echo "<h3>". $_SESSION['uname']."</h3>";
    header("Location: ../index.php?login=success");

    exit();

  }
  else{
    header("Location: ../login.php?login=errorpassfail");
    exit();
  }
  }
}
 }
}

   else{
    header("Location: ../login.php?login=error");
   }
?>