Jump to content

[SOLVED] Session problems


timbrown

Recommended Posts

Hello,

 

I've got a problem with sessions. Sessions work fine on one page, but when I navigate to another page they don't appear to be set. I'm sure there's somethign simple i'm doing wrong but I've been trawling around for some time now and got nowhere.

 

Any help would be great!

 

Here's the code simplified -

 

page 1...

works fine

<?php

session_start();

 

$_SESSION["name"] = "tim";

 

echo $_SESSION["name"];

?>

<a href="page2">go there</a>

 

page 2...

says the session variable is not set

 

<?php

echo $_SESSION["name"];

 

?>

 

 

Link to comment
Share on other sites

check the session_save_path variable in your php.ini.  Sometime it pointed to an invalid directory path and caused session not working.  Set it to a valid directory with write permission.

 

if it still doesn't work, please say so

Link to comment
Share on other sites

check the session_save_path variable in your php.ini.  Sometime it pointed to an invalid directory path and caused session not working.  Set it to a valid directory with write permission.

 

if it still doesn't work, please say so

 

Hi,

 

thanks for the reply. my session.save_path points to c:\php\sessiondata and there are session files in there.

 

I did a test and I can see when I refresh page1, a new file is made.

Link to comment
Share on other sites

I have tried that too, but it then says that the 'name' variable is not set. Does session_start() begin a new session each time?

 

Obviously I have missed out the very basic of session.  You must called session_start() at the very beginning of all the scripts that use $_SESSION. 

 

Another important point you need to know is placing the php script tag '<?php' at the first character of the first line of the .php file.  Meaning no any character, space ..etc before it.

 

 

Link to comment
Share on other sites

I have tried that too, but it then says that the 'name' variable is not set. Does session_start() begin a new session each time?

 

Obviously I have missed out the very basic of session.  You must called session_start() at the very beginning of all the scripts that use $_SESSION. 

 

Another important point you need to know is placing the php script tag '<?php' at the first character of the first line of the .php file.  Meaning no any character, space ..etc before it.

 

 

 

Hi, still no joy.

 

my 2 files now look like this -

 

page 1

 

<?php

session_start();

$_SESSION["name"] = "tim";

 

?>

<html>

<a href="page2.php">go</a>

</html>

 

page 2

 

<?php

session_start();

echo $_SESSION["name"];

?>

 

and I still get the message: Undefined index: name.

 

any new ideas?

Link to comment
Share on other sites

Try using single quotes as well as using print_r to see if session data is there.

 

page 1

 

<?php

session_start();

$_SESSION['name'] = "tim";

print_r($_SESSION);

 

?>

<html>

<a href="page2.php">go[/url]

</html>

 

page 2

 

<?php

session_start();

echo $_SESSION['name'];

print_r($_SESSION);

?>

 

Link to comment
Share on other sites

Try using single quotes as well as using print_r to see if session data is there.

 

page 1

 

<?php

session_start();

$_SESSION['name'] = "tim";

print_r($_SESSION);

 

?>

<html>

<a href="page2.php">go[/url]

</html>

 

page 2

 

<?php

session_start();

echo $_SESSION['name'];

print_r($_SESSION);

?>

 

 

Hi,

 

page 1 outputs

Array ( [name] => tim )

 

page 2 outputs

Array ()

Link to comment
Share on other sites

By the way, the windows servers often have a default setting in PHP that needs to be changed:

 

Try commenting this out in the php.ini

session.use_only_cookies = 1

 

So that line should be:

; session.use_only_cookies = 1

 

Restart your server then try it.

 

I checked and this line was already commented out.

Link to comment
Share on other sites

I'm going to reuse a post I posted before :D

-----

Create a page with this code:

 

 

Code:

<?php
session_start();
if (isset($_SESSION['count']))
	{
	$_SESSION['count']++;
	}
else
	{
	$_SESSION['count']=0;
	}
echo $_SESSION['count'];
?>

 

Then, go to that page. Refresh it a few times. See if the number it outputs increases. If it does increase, there is a problem with your script, if it doesn't, there is most likely a problem with your PHP session temp dir or a setting in php.ini

Link to comment
Share on other sites

I'm going to reuse a post I posted before :D

-----

Create a page with this code:

 

 

Code:

<?php
session_start();
if (isset($_SESSION['count']))
	{
	$_SESSION['count']++;
	}
else
	{
	$_SESSION['count']=0;
	}
echo $_SESSION['count'];
?>

 

Then, go to that page. Refresh it a few times. See if the number it outputs increases. If it does increase, there is a problem with your script, if it doesn't, there is most likely a problem with your PHP session temp dir or a setting in php.ini

 

Thanks for that post.

 

the number just stays at 0 when I refresh.

 

my php.ini file refers to session.save_path as c:\php\sessiondata, and that has all persmissions set on it.

 

any other php.ini danger spots?

Link to comment
Share on other sites

Hi Tom, I tried your script and it did not increment.

 

These are my session settings :

 

session.save_handler = files

session.save_path = "C:/php/tmp"

session.use_cookies = false

session.name = PHPSESSID

session.auto_start = 0

session.cookie_lifetime = 20

session.cookie_path = /

session.cookie_domain =

session.serialize_handler = php

session.gc_probability = 10

session.gc_divisor    = 1000

session.gc_maxlifetime = 9000

session.bug_compat_42 = 0

session.bug_compat_warn = 1

session.referer_check =

session.entropy_file =

session.cache_limiter = nocache

session.cache_expire = 180

session.use_trans_sid = 0

session.hash_function = 0

session.hash_bits_per_character = 5

url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"

 

Any ideas, where I have gone wrong?

Link to comment
Share on other sites

scottybwoy is different from timbrown, sorry to hijack the thread, but I am suffering the same problem.  And why is it ugly, I just selected the settings I have set, cos I'm sure everyone has the  same comments in their php.ini file.  And isn't it down to the provider how they want their sessions handled.  But I can see that mine isn't working how I want it too, any pointers would be helpful.

Link to comment
Share on other sites

this is my (timbrown's) session settings...

 

session.auto_start Off

session.bug_compat_42 On

session.bug_compat_warn On

session.cache_expire 180

session.cache_limiter nocache

session.cookie_domain no value

session.cookie_lifetime 0

session.cookie_path /windows/temp

session.cookie_secure Off

session.entropy_file no value

session.entropy_length 0

session.gc_divisor 100

session.gc_maxlifetime 1440

session.gc_probability 1

session.hash_bits_per_character 4

session.hash_function 0

session.name PHPSESSID

session.referer_check no value

session.save_handler files

session.save_path C:\PHP\sessiondata

session.serialize_handler php

session.use_cookies On

session.use_only_cookies Off

session.use_trans_sid 0

 

Link to comment
Share on other sites

I have to get back to work cause i keep spending to much time on these boards... :D So I am going to post my session variables. This is working on my system, you can try to mix and match and see what might be causing it.

 

session.auto_start Off Off

session.bug_compat_42 Off Off

session.bug_compat_warn On On

session.cache_expire 180 180

session.cache_limiter nocache nocache

session.cookie_domain no value no value

session.cookie_httponly Off Off

session.cookie_lifetime 0 0

session.cookie_path / /

session.cookie_secure Off Off

session.entropy_file no value no value

session.entropy_length 0 0

session.gc_divisor 1000 1000

session.gc_maxlifetime 1440 1440

session.gc_probability 1 1

session.hash_bits_per_character 5 5

session.hash_function 0 0

session.name PHPSESSID PHPSESSID

session.referer_check no value no value

session.save_handler files files

session.save_path C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload

session.serialize_handler php php

session.use_cookies On On

session.use_only_cookies Off Off

session.use_trans_sid 0 0

Link to comment
Share on other sites

I have to get back to work cause i keep spending to much time on these boards... :D So I am going to post my session variables. This is working on my system, you can try to mix and match and see what might be causing it.

 

session.auto_start Off Off

session.bug_compat_42 Off Off

session.bug_compat_warn On On

session.cache_expire 180 180

session.cache_limiter nocache nocache

session.cookie_domain no value no value

session.cookie_httponly Off Off

session.cookie_lifetime 0 0

session.cookie_path / /

session.cookie_secure Off Off

session.entropy_file no value no value

session.entropy_length 0 0

session.gc_divisor 1000 1000

session.gc_maxlifetime 1440 1440

session.gc_probability 1 1

session.hash_bits_per_character 5 5

session.hash_function 0 0

session.name PHPSESSID PHPSESSID

session.referer_check no value no value

session.save_handler files files

session.save_path C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\php\upload

session.serialize_handler php php

session.use_cookies On On

session.use_only_cookies Off Off

session.use_trans_sid 0 0

 

 

hey tom thanks so much. It works now!!!!!!!

 

I just changed my cookie_path to / which is the default anyway.

 

Hope you haven't jeopardised you paid emplyment helping me!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.