| |||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| NamePros Regular | Help with sessions Hi it's me again 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.
__________________ C905 - Sony Ericsson mobile phone with an 8 megapixel camera! |
| |
| | #2 (permalink) |
| NamePros Member | 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! |
| |
| | #3 (permalink) |
| NamePros Regular | 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 Thanks for trying though
__________________ C905 - Sony Ericsson mobile phone with an 8 megapixel camera! |
| |
| | #4 (permalink) |
| NamePros Member | 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... |
| |
| | #5 (permalink) |
| Senior Member | 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) |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |