| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Regular Join Date: Aug 2005 Location: Leeds, UK
Posts: 263
![]() | 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.
__________________ Michael McIntyre - Fan site for the super funny UK comedian Michael McIntyre |
| |
| | #2 (permalink) |
| NamePros Member Join Date: May 2006
Posts: 160
![]() | 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! |
| |
| | THREAD STARTER #3 (permalink) |
| NamePros Regular Join Date: Aug 2005 Location: Leeds, UK
Posts: 263
![]() | 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
__________________ Michael McIntyre - Fan site for the super funny UK comedian Michael McIntyre |
| |
| | #4 (permalink) |
| NamePros Member Join Date: May 2006
Posts: 160
![]() | 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) |
| NamePros Expert Join Date: Nov 2003 Location: Scotland
Posts: 5,069
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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) |
| |