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.

Loosing $_SESSION Variables on Page Refresh

Featured Replies

Hello,

 

I have created a login system that works fine. I am carrying $_SESSION variables over which are used to populate content based on the particular user that has logged in. Everything seems to be working fine... Until I hit refresh. Doing this looses the $_SESSION variables.

 

I have a php function running at the begging of my home page (The page users are directed to when they log in) which checks to see if a particular $_SESSION variable is set. If it is (Which it will be when a user logs in) nothing happens. If this same $_SESSION is not set then it redirects to the index page. I put this in place so that if someone types /home.php into the URL it won't allow them. It also stops being logging out and hitting back on the browser to go back to the home page.

 

Hitting refresh on the home page brings the user to the index page. I ran debug and as soon as I hot refresh my $_SESSION variables were lost. I have session_start() at the top os all my pages so that is not the issue. I do not have session_destroy anywhere and I have session_write_close after setting my $_SESSION varibales.

 

I have looked all over the web and have been unable to find an answer to this. Any, and all, help will be highly appreciated. This is the code that I have at the top of my home page, along with the function.

 

<?php
$pageTitle = "Journal Vault";
$section = "home";
session_start();
include('inc/functions.php');
redirect();
include('inc/header.php');
include('inc/navigation.php');
and here is said function
function redirect() {
if(!isset($_SESSION['username'])) {
header("Location: index.php");
exit();
}
After logging in I checked the saved cookies and my $_SESSION was there. Even after refreshing the COOKIE was still there. Help please.
- MjA -

 

Need to see the relevant source code to work this one out. You must be destroying the session data somewhere, or overwriting it.

 

Can you paste the source code somewhere, or host on GitHub temporarily?

  • Author

I set my $_SESSION variables during my login execution phase. This is the code being used

 

if($result) {
if(mysql_num_rows($result) > 0) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['clientid'] = $member['ClientID'];
$_SESSION['username'] = $member['Username'];
$_SESSION['password'] = $member['Password'];
session_write_close();
header("location: home.php");
exit();
} else {
//Login failed
$errmsg_arr[] = 'user name and password not found';
$errflag = true;
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
header("location: login.php");
exit();
}
}
}else {
die("Query failed");
}
EDIT
I have discovered now that it's not entirely based on just page refresh but actually any other page once its moved from index to home. I create the $_SESSION variables and it uses these to log in. It then uses these variables to populate some fields and tables on the home page. This all works fine. But then moving away to any other page loses the $_SESSION variables.
This is the code I am using to navigate pages. Could this be the root cause of any of it?
<button onclick="window.location.href='my_account.php'" class="buttonextra1 big grey">Account Overview</button>
- MjA -

Edited by SkullBob

One way i debug web applications is simply remove little bits of the applications code that i think may be causing the problem. If it fixes the issue then i leave it removed if it doesn't i simply put that piece of code back. Btw you don't really need to use session_write_close() . As stated from the php.net

 

Session data is usually stored after your script terminated without the
need to call session_write_close()

I need to see the full application code for a index or home 'load' if that makes sense. That snippet looks like it should work. You're using deprecated methods, but that's not the reason for this, and it looks like you're just learning so it's fine for now.

 

I need to see full code for index and home.php, including all includes. If you host it on GitHub temporarily, that's probably easiest. It's the only way I can find the problem.

Edited by andy9l

  • Author

After much persistence I managed to figure out what was causing the issue, thanks to webdesigner93's suggestion of removing code bit by bit and testing.

 

I have my navigation part of the site in an include php file. In this I have code for a log out button. In this code I included php code to destroy the current session when the log out button was pressed. When this navigation was first included in the home page everything worked fine. But for some reason when refreshing, or navigating away from the page, meaning the incoude had to be reloaded, it would run the php code for some reason. I don't know if it was the correct thing to do but my workaround is as follows;

 

<?php
session_start();
session_destroy();
header("Location: index.php");
This code is in it's own php page which is only accessible when the user clicks on log out and is directed to this page.
I do not know why the php code was running before when loaded for a second time. Any insight into this?
@andy9l - I am aware I am using deprecated code. The book I used to learn the basics from was a few years old and I figured as a first php project it would not matter so much. Out of curiosity, what sort of problems could I run into using such code?
- MjA -

Edited by SkullBob

@andy9l - I am aware I am using deprecated code. The book I used to learn the basics from was a few years old and I figured as a first php project it would not matter so much. Out of curiosity, what sort of problems could I run into using such code?

Best not to get into bad practices though, hey?

 

As for immediate problems - security risks, and eventually the code won't work at all when it's phased out of future PHP versions,

  • Jo 90 locked this topic
Guest
This topic is now closed to further replies.

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.