0

I'm new to PHP and am even more of a beginner when it comes to sessions. I have my index.php page, which is where users can register and login. The forms are posting to submit.php, respectively for registering and logging in.

This is the dashboard.php file where i showed the username of a user and email address from which account he's login.

<?php
session_start();
include "includes/config.php";
include "layouts/header.php";
$s_title = "Superior Results";
if(isset($_SESSION['id'])) { 
    $username = $_SESSION['username'];
    $email = $_SESSION['email'];
    $id = $_SESSION['id'];

} else {
    header('Location: index.php');
    die();
}
 $sql = "SELECT email, username FROM members";
 $result = mysqli_query($dbCon, $sql);

 $row = mysqli_fetch_assoc($result);

?>

And this is how i call these session variables in dashboard.php file

<?php echo $_SESSION['username'];?>   
<?php echo $_SESSION['email'];?>   

Username works but email didn't work it shows Notice: Undefined index: email

screenshot

8
  • Yes, and where do you set those $_SESSION variables? I'm missing something like $_SESSION['email'] = $row['email']. Commented Mar 4, 2017 at 0:37
  • Where did you set the value of $_SESSION['email']? Are you sure you did it? Commented Mar 4, 2017 at 0:38
  • i didn't set those variables anywhere... it should fetch from sessions of the user account. sorry im little confused with this Commented Mar 4, 2017 at 0:39
  • @VictorArcas In the Netherlands we call this spuit elf. No worries, a translation will not help you. Commented Mar 4, 2017 at 0:39
  • @VictorArcas no i didn't set any value... it should fetch according to which account the user has logged in. Commented Mar 4, 2017 at 0:41

2 Answers 2

1

The reason for this error is that you're trying to read an array key that doesn't exist. The isset() function is there so you can test for this. There's no need for null checks as you never assign null to an element:

// check that the 'email' key exists
if (isset($_SESSION['email'])) {
  // it does; output the message
  echo $_SESSION['email'];

  // remove the key so we don't keep outputting the message
  unset($_SESSION['email']);
}

Well, just for your comments below, I´m not sure if I understand your needs, but you want to do something like this:?

$username = $_POST['username'];

$sql = "SELECT email FROM users WHERE username = '$username'";
if(($result = mysqli_query($conn, $sql) != false){
  if(($row = $result->fetch_assoc() !== null)){
    $_SESSION['email'] = $row;
  } else {
    echo 'no rows in database';
  }
} else {
  echo 'You have an error in you mysql syntax';
}
//.. work with concrete user
Sign up to request clarification or add additional context in comments.

7 Comments

Please explain how to set it perhaps?
session_start(); $_SESSION['email'] = "[email protected]"; echo $_SESSION['email'];
it redirected me to my index.php page and didn't let me login with my username/password i.imgur.com/CgOepR0.png when i isset email..
@NoorQureshi Great, keep in mind a $_SESSION is client specific !
Because you responded and resolved the issue with it, +1
|
0

after the user logs in you need to set the session variables

 $sql = "SELECT email, username FROM members WHERE id = '".$confirmed login id from the login process."' ";
 $result = mysqli_query($dbCon, $sql);

 $row = mysqli_fetch_assoc($result);

$SESSION['id'] = $row['id'];
$SESSION['email'] = $row['email'];
$SESSION['username'] = $row['username'];

Once those session variables are set after they log in, you can use them on any other page that is part of that session (has session_start() on the first line)

7 Comments

When i did login from another account which includes different email it still showed me my first account email address.. i.imgur.com/jZgtBih.png .... i.imgur.com/lBomGSQ.png .... i.imgur.com/dkBpxfx.png
@NoorQureshi If you would delete your PHPSESSID cookie in your browser, that issue will likely be resolved as this code would work if you set the id in this query correctly.
@Xorifelse yes i did it didn't worked.. I'm doing something wrong here: i.imgur.com/xZvzhzk.png ..... the username output seems to be working correctly and 'email' causing issue...
@guesterator_php If you'd update the code with error correction and properly fetch() for 1 expected result (as I didn't in my edit), I'll upvote. Also, you're selecting email and username and not $row['id'] which shouldn't happen in the first place because that is already known.
@NoorQureshi Because I'm not seeing a WHERE clause in that picture. that means it selects every row from your table. Since you should only expect 1 result from 1 user id, that should not happen.
|