Unstoppable Domains

Help with sessions

Spaceship Spaceship
Watch

noswad

Established Member
Impact
1
Hi it's me again :hehe:

I have a page using sessions to obtain data from my database.

For example -

session_start();
echo("Premium Membership: $_SESSION[membership] ");

So if a user has a premium membership, it will display:

'Premium Membership: Yes'

If the databse field for membership is empty, is there a script to auto display:

'Premium Membership: No'

Thanks.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
It's probably best to just do a test at the start of your script and set it to 'no' if it's not found. That way, you won't have to continually test to see if it is set.

Do something like:
if (! isset( $_SESSION['membership'] ) ) {
$_SESSION['membership'] = 'no';
}

Hope that helps!
 
0
•••
Hi this works but there are a few problems...

For example:

When a user logs in, if the membership field is empty, the page displays:

'Premium Membership: '

Then when the page is refreshed it corrects itself to:

'Premium Membership: No'

However, when the user logs in and the membership field is 'Yes', the page displays:

'Premium Membership: Yes'

...but when the page is refreshed it changes to:

'Premium Membership: No'

lol :hehe:

Thanks for trying though :)
 
0
•••
When exactly are you running that code that I put there?

It obviously needs to be between the two lines that you started with, but it almost looks as though it's been placed after the echo.

To test the problem of after refreshing reverting to 'no', for the time being you should use a different value for 'unset' session variables. That way you'll know pretty quickly on the refresh whether it's saying 'no' because it's been overwritten with 'no' or because it's just not remembering that it's set...
 
0
•••
the reason the problem happens on the first visit is because session data cannot beused in the script where it is set that is why it works on refresh as it was set on the previous initialisation of that page.

A way around this is after the session data has been set use a header redirect to the page you want them to see (making sure you attache the session id to the end of the address you send them as php does not automatically put attach this when you do a header redirect)
 
0
•••

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back