Jump to content


PHP sessions acting really odd!


  • Please log in to reply
9 replies to this topic

#1 Guest_jgp4_*

Guest_jgp4_*
  • Guests

Posted 01 July 2006 - 09:15 PM

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.

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 01 July 2006 - 11:07 PM

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....html#msg157705


#3 Guest_jgp4_*

Guest_jgp4_*
  • Guests

Posted 02 July 2006 - 06:13 AM

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.

#4 Guest_jgp4_*

Guest_jgp4_*
  • Guests

Posted 02 July 2006 - 06:24 AM

EDIT: sorry I meant 'PHP can't see that there's a session'

sorry if it caused any confusion

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 July 2006 - 06:34 AM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 Guest_jgp4_*

Guest_jgp4_*
  • Guests

Posted 02 July 2006 - 11:06 AM

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

#7 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 02 July 2006 - 11:10 AM

If I press refresh on that page you gave it does go up by 1... What's the problem then?

Orio.
Think you're smarty?

(Gone until 20 to November)

#8 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 02 July 2006 - 01:54 PM

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.



#9 Guest_jgp4_*

Guest_jgp4_*
  • Guests

Posted 03 July 2006 - 07:06 AM

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?

#10 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 03 July 2006 - 03:07 PM

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?

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.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users