Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest jgp4

PHP sessions acting really odd!

Recommended Posts

Guest jgp4
I really need some help. I've just starting implementing sessions on a CMS i'm working on, but they're not working on my testing server (winXPpro, apache 2, php 4.4). Every time the page calls session_start() it creates a brand new session and cookie. the script works fine on my server at http://jgp4.co.uk/sessions.php .

The script is just the basic one from PHP.net about counting the number of times a page has been loaded by the client.

Any help would be really appreciated.

Share this post


Link to post
Share on other sites
It's best to post the exact code so members on this forum can help you better.

Check that the session save path is set correctly in the php.ini file.

See our session troubleshooting guide:
http://www.phpfreaks.com/forums/index.php/topic,31047.msg157705.html#msg157705

Share this post


Link to post
Share on other sites
Guest jgp4
Sorry about that, here's the code:

<?php
session_start();

if(!isset($_SESSION['count'])) {

    $_SESSION['count'] = 0;
}
else {

    $_SESSION['count']++;
}

echo $_SESSION['count'];

?>

The session save path is set to "C:\php\sessions" and seems to be working, every time I refresh the page it create a new file in that dir and the value for the localhost cookie PHPSESSID changes to the values of the new file.

It seems like php can see that there's a session and so creates a new one, can anyone think why?

Thanks in advance.

Share this post


Link to post
Share on other sites
Guest jgp4
EDIT: sorry I meant 'PHP [b]can't[/b] see that there's a session'

sorry if it caused any confusion

Share this post


Link to post
Share on other sites
i tried your code works fine and you link also works ok .


look at the sever php.ini to match the testing php.ini ok

the code you provided only counts up from 0 to whatever when refresing the browser
but if you go away from the page the session resets and counts agin from 0  ok

unrealated
if you wanted to add a hit counter to the members table then just use

$hits+1;
insert value ('hits', where id=$id");

then the data base will insert hits for the member and add one each time ok.

Share this post


Link to post
Share on other sites
Guest jgp4
I don't have access to the server phpini but i ran a phpinfo() and everything is set up the same except for the dir where the sessions are saved as its a linux server. The sessions folder is available to all users etc. with full permissions. Could this just be a bug with running it on windows? if you want to have a look the info is at http://jgp4.co.uk/phpinfo.php

Share this post


Link to post
Share on other sites
If I press refresh on that page you gave it does go up by 1... What's the problem then?

Orio.

Share this post


Link to post
Share on other sites
jgp4, have you gone through the session troubleshooting guide?

It sounds like the browser you're using isn't saving the php session cookie. It could also be network security software you have installed. All this is specified in the troubleshooting guide.

Try the session script in about the 10th bullet and report back what you get. If the session ID is shown in the URL, then session cookies are not being saved on your Windows system.

Share this post


Link to post
Share on other sites
Guest jgp4
I've tried the script in the troubleshooting guide and it works, but displays the sessid in the url, i'll keep reading the guide but i think i've tried avrything there. If it's not setting cookies then what are the files which keep getting created in my session.save_path?

Share this post


Link to post
Share on other sites
[quote author=jgp4 link=topic=99134.msg390894#msg390894 date=1151910410]
I've tried the script in the troubleshooting guide and it works, but displays the sessid in the url, i'll keep reading the guide but i think i've tried avrything there. If it's not setting cookies then what are the files which keep getting created in my session.save_path?
[/quote]
The SID constant doesn't get populated by PHP unless it can't write a session cookie. A PHP session cookie holds the session ID (usually 32 characters long). The files created in your session path is what actually holds the data you wish to save per session. The server side session filename is in the form of sess_xxx, where "xxx" is the session ID.

The cookie has the session ID so PHP knows which session this is and can get at any existing session file data located in your session save path. If the session cookies is not there, or the session ID is not provided in the URL, then PHP will think it's a new session. And that's why you keep getting a new session ID each page refresh.

When session.trans_sid is on, PHP automatically adds the session name and ID to each URL when a session cookie couldn't be created (so the session is not lost). However, PHP doesn't do it on redirects like using header with location. That's why my example code uses the SID constant.

You have to see why cookies aren't being created (common reasons are outlined in the troubleshooting guide). Also, try different browsers.


Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.