Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empty Session Array

Featured Replies

I'm trying to create a session using a login script which assigns pulls a username and an id from the members database and assigns them to $_SESSION variables like so... (just a snippet).

 

$resource = mysql_query("SELECT id,username FROM members WHERE username='{$_POST['username']}' AND password='".md5($_POST['password'])."'");
 $row = mysql_fetch_assoc($resource);

 if($row['username'])
 {
  // Store some data in session
  $_SESSION['username'] = $row['username'];
  $_SESSION['id'] = $row['id'];
  header("Location: update.php");
  exit;
 }
 else $err[]='Wrong username and/or password!';

 

It appears to work well and redirect me if the login is successful but when trying to print_r the session on the next page (update.php) it's giving me an empty array as if the session never existed (I've also tried echoing the session_name but that also defaults).

 

I'm not sure why this is happening and to troubleshoot I've both echo'd and print_r'd the session on the login page (logging in and then going back to my login page) and it displays the contents perfectly so why aren't they displaying on the update.php page? I've got session_start() at the very top of my update.php but it doesn't seem to resume it.

 

I've tried not to include too much information not to overwhelm but I can provide more if requested; I'm thinking that this may just be a newbie mistake (I'm still learning PHP).

  • Author

Yeah, that was my initial diagnosis but it's there on both of them (login.php and update.php) before any HTML output.

If you'r right about the session_start() and everything should work in your oppinion I advice you to check these:

- bad server settings: check your php settings with phpinfo() function and search for "session" maybe you notice something,

- you may using old php version, if so the session_register() function could help, check it on php.net.

try printing the values of $row before redirecting to see if there being set also make sure you sanitise your username in the SQL

  • Author

If you'r right about the session_start() and everything should work in your oppinion I advice you to check these:

- bad server settings: check your php settings with phpinfo() function and search for "session" maybe you notice something,

- you may using old php version, if so the session_register() function could help, check it on php.net.

 

I'll post a screenshot of my PHP settings after my food but as for my version, it is 5.4.3.

 

try printing the values of $row before redirecting to see if there being set also make sure you sanitise your username in the SQL

 

I can print the values of $row if inserted at the position shown below but any later in the code and it doesn't work but instead I'm still able to print the values of the session which works fine. So $row seems to unset where $_SESSION is assigned the variables of $row but on the next page, $_SESSION doesn't even appear to exist -.-"

 

...

if(!count($err)){

$_POST['username'] = mysql_real_escape_string($_POST['username']);
$_POST['password'] = mysql_real_escape_string($_POST['password']);

$resource = mysql_query("SELECT id,username FROM members WHERE username='{$_POST['username']}' AND password='".md5($_POST['password'])."'");
$row = mysql_fetch_assoc($resource);
if($row['username'])
{
// Store some data in session
$_SESSION['username'] = $row['username'];
$_SESSION['id'] = $row['id'];
print_r($row); // <------ IF INSERTED HERE <-----
//header("Location: update.php");
}
else $err[]='Wrong username and/or password!';
}
if($err)
// Save the error message in the session
$_SESSION['msg']['login-err'] = implode('<br />',$err);
}
?>

Edited by Alluziion

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.