How to use sessions in php - what's wrong with this code?

Make sure you're not calling unset anywhere and make sure cookies are enabled.

Up vote 0 down vote favorite share g+ share fb share tw.

I am trying to set the session and increment the session on every refresh. But its always returning "1" for $_SESSION'views'. What is wrong with this code?

Php session link|improve this question edited May 12 '09 at 7:21Paolo Bergantino89.1k17232276 asked May 12 '09 at 7:19praveenjayapal1,01152749 44% accept rate.

Indent code 4 spaces next time. Also, I just pasted exactly what you have there and it works for me. – Paolo Bergantino May 12 '09 at 7:22 Your browser might be rejecting cookies for security/privacy reasons.

Check your cookies and make sure one is there for your domain. – cletus May 12 '09 at 7:41.

Make sure you're not calling unset anywhere and make sure cookies are enabled. I uploaded that snippet as-is: Click here to test... It works fine for me. Try it.

Hit refresh a few times. If it doesn't work for you it's your browser settings. If it works for you on my server but not on yours then there's a problem with your php installation.

Check your php. Ini, check if cookies are enabled in your browser. I've tried your code and worked for me.

Do I need to configure any thing in php. Ini – praveenjayapal May 12 '09 at 7:29 Have you changed the session variables? If not, the default settings would be fine.

Have a look at the cookies. Is there a PHPSESSID or something named like this? – Vili May 12 '09 at 14:31.

It might be the browser cache, try Ctrl+F5. There's nothing wrong with your code.

Try: echo $_SESSION'views' directly after session_start(); and examin the output. Make sure you're not destroying the session in this or any other scripts.

As a tip for other folks with same problems: Had the same problem just now - and found out why, with the above test snippet. Watch out for any modrewrites! I had a bad modrewrite for my page, which caused the index.

Php to be loaded two times (one time for redirect, and once for the actual display) on every refresh. Try renaming your file to some awkward name and test it again.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions