Click to See Complete Forum and Search --> : Can't seem to get sessions to work.


Phrank
06-23-2007, 06:29 PM
If I use the default directory in the php.ini (C:\PHP\sessiondata) my session works fine. If I set the path to a different dir (cgi-bin\tmp) it writes the session file there properly but puts nothing in it.

I need to be able to define the dir when I take the program to my Internet Provider. They give you a special dir. I have tried it and get the same results as above.

I have made sure the dir is not read only and have even shared it on my system to no avail.

Not sure what to try next.

Any Idea?

TIA

My code.

session_save_path("cgi-bin\tmp");
@session_start();
echo "save path = " . session_save_path();

bradgrafelman
06-24-2007, 06:24 PM
What O/S and webserver are you running? Sounds like a permissions problem.

Phrank
06-25-2007, 12:29 AM
Win XP and IIS.

Phrank
06-25-2007, 12:29 AM
Where was the post moved to? It's important to me.

bradgrafelman
06-25-2007, 02:16 AM
Sorry - moved it to the Install forum since it sounds like something went wrong with the install process.

Is it important that you use IIS? If not, I would suggest using something like Apache (http://httpd.apache.org) for a webserver.

Otherwise... you didn't say if it was Pro or not, but I still wonder if it isn't a permissions and/or path problem:

Do you realize that "cgi-bin\tmp" is a relative path, so if your script is located at http://mysite.com/script.php then it will attempt to create a file in http://mysite.com/cgi-bin/tmp/ , but if another script is at http://mysite.com/subdir/script2.php then the same path will point to http://mysite.com/subdir/cgi-bin/tmp/ ?

In addition, don't use forward slashes. If you used double quotes and the path you showed us, there's probably a tab character being inserted in there. Try using forward slashes instead.

Phrank
06-25-2007, 04:16 PM
Thanks for responding.

I am testing sessions on my local pc for a captcha fix for a guestbook.

I am using XP Pro/IIS6/Php 4.3.7/Dreamweaver CS3.

The session.save_path in my php.ini has C:\PHP\sessiondata.

In my guestbook form I have:

@session_start();

just before my <body> tag. On submit I goto addguestbookentry.php where I test to see if the captcha var sent by the post method matches the same var sent by the session.

All works well until I put the pages up on the server. They require a path.

So getting that to work locally first I put in:

session_save_path("C:\PHP\sessiondata");
@session_start();

and that works great. The Php.ini and my path are the same. If I try to use a dir I created it opens the session file in that path but writes no data.

In that tmp dir I made sure it is shared with write permissions. I right clicked on the dir and made sure it has all permissions and then also 'shared' via windows explorer.

Doesn't help. Went ahead for a test and 'shared' my whole c drive and that didn't help.

Tried using session_save_path("C:\PHP\sessiondata\tmp"); just to see if that would work but still only opens the sesson file but does not write anything. Of course I 'shared' the tmp dir and gave it all permissions.

Seems like the above should work but things only work if I match the dir with the one stated in the php.ini.

Another funny thing that is bugging me. If I use:

session_save_path("C:\PHP\sessiondata\tmp");

and echo the path I get c:\PHP\sessiondata mp.

If I use session_save_path("C:\PHP\sessiondata\\tmp"); I get

c:\PHP\sessiondata\tmp.

Why do I not need to put "c:\\PHP\\sessionsdata"?

Anyway I tried using

"c:/PHP/sessiondata/tmp" and that does not work either.

Am a bit confused.

Any help would be greatly apprecaited.

TIA

bradgrafelman
06-29-2007, 04:03 PM
Why do I not need to put "c:\\PHP\\sessionsdata"?Because neither "P" nor "s" are part of any special backslash sequences (ex. "\n" for a new line, "\t" for a horizontal tab). To avoid such issues, I tend to always use forward slashes.

In my guestbook form I have:

@session_start();Well if you're having an issue with something not working right, why would you want to suppress any helpful error messages that PHP might be trying to give you? Remove the @ signs from any session-related line - we want to see any and all error messages PHP can give us. (Speaking of - have you ensured that display_errors is set to On and error_reporting set to E_ALL ?)

Of course I 'shared' the tmp dir and gave it all permissions.You keep talking about sharing the folder, though I don't know why. Sharing the folder does absolutely nothing to change the permissions of the folder (thus it doesn't help PHP access it in any way). When you say you "gave it all permissions", what does this mean? Did you go to the Security tab (not Sharing) and give the "Everyone" user full control? If not, what did you do?

If I try to use a dir I created it opens the session file in that path but writes no data.Okay, so let's try something like this:

<?php

$path = 'path/to/your/session/dir/test_file.txt';

$fp = fopen($path, 'w') or die('couldn\'t open file');
fwrite($fp, 'test');
fclose($fp);
echo 'Successfully wrote data!<br><br>Attempting to read data from file... ';

$data = file_get_contents($path) or die('FAILED! (Couldn\'t read file)');
if($data == 'test') echo 'SUCCESS!'; else echo 'FAILED! (Data did not match)';

?>Basically, this script emulates what happens when you start a session; it attempts to open a file in the directory where you're trying to store sessions (make sure you double check this path to ensure it's what you're trying to use in your scripts), write data to it, and then read back that file's contents.

riddellchris
07-12-2007, 11:07 AM
I have tried all of the above and am still trying to pass session variables from one page to another and have found some code from a book to check but even this code doesn't seem to work. I'm running php with apache 2.2.4 and windows vista and internet explorer 7. I think that the issue is with my php.ini file in c:\php.ini

The first site is movie1.php

<?php
session_start();
$_SESSION['username'] = "Joe12345";
$_SESSION['authuser'] = 1;
?>

<html>
<head>
<title>Find my Favourite Movie</title>
</head>
<body>
<?php
$myfavmovie = urlencode("Life of Brian");
echo "<a href='moviesite.php?favmovie=$myfavmovie'>";
echo "Click here to see information about my favourite movie!";
echo "</a>";
?>
</body>
</html>

the second is moviesite.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', True);


session_start();
if ($_SESSION['authuser'] ! = 1) {
echo "Sorry, but you don't have permission to view this page, you loser!";
exit();
}
?>

<html>
<head>
<title>My Movie Site - <?php echo $_REQUEST['favmovie']; ?></title>
</head>
<body>
<?php
echo "Welcome to our site, ";
echo $_SESSION['username'];
echo "! <br>";
echo "My favorite movie is ";
echo $_REQUEST['favmovie'];
echo "<br>";
$movierate = 5;
echo "My movie rating for this movie is ";
echo $movierate;
?>
</body>
</html>

from this i still don't get errors even though it says on this site that I will. It thought that I had configured the php.ini file correctly according to my book and other forum posts etc and I have tried all the forum posts but I am stuck please help!

bradgrafelman
07-12-2007, 09:02 PM
What is session.save_path set to in your php.ini file, and does a phpinfo() output show this value?

EDIT: Also, did you try using my test script above to make sure that PHP can write to the session directory?

riddellchris
07-13-2007, 03:55 AM
Ah thanks so much! I had tried your test script but i didn't get it working nor find the error. I changed my session.save_path after checking phpinfo() that's brilliant by the way thanks! then after re-running i found the error in the script i had put a space between ! = so once fixed it worked! Thanks a lot!