Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session - again

Status
Not open for further replies.

greatwhitenorth98

Technical User
Apr 8, 2004
32
FR
Yep, I'm a Newbie - with session problems. The code below, while relatively simple, does not keep the session variable to the next page. In fact, when looking at the temp file where the session ids are kept, there is one session generated by loading the first page, another when going from the first to the second and a third one going back to the first page. Regardless (and probably due to), the $_SESSION variable is not kept and echoed to the screen.

Please, someone tell me what is going on!? I'm going bananas here! I've looked throughout the forum and applied the ideas that I have found - to no avail.

Thanks a heap!

--------------------------
index.htm
--------------------------

<?php
session_start();

$_SESSION['user'] = "Hubert";

echo "(IDX) Session user: " .$_SESSION['user']. "<br>";

if(isset($_POST['session_check'])) {
header("Location: session_check.htm");
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Session test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']?>" method="post">
<br><br>
<input type="submit" name="session_check" value="Check Session variables">
</form>

</body>
</html>

--------------------------
session_check.htm
--------------------------

<?php
session_start();

if(isset($_POST['home'])) {
unset($_SESSION['user']);
header("Location: index.htm");
}
echo "(SC) Session user: " .$_SESSION['user']. "<br>";
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Session test - Page 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']?>" method="post">
<br><br>
<input type="submit" name="home" value="Home">
</form>

</body>

-------------------------
 
Thanks for answering!

I have installed the latest version of the phpDev package which includes Apache, mySQL and PHP (plus I installed PEAR) on my WIN2KPro machine.

greatwhitenorth98

Does the code run properly on your system? Maybe I have a configuration issue?
 
The reason I asked about the web server is that IIS doesn't send cookies when you use the "Location:" header.

With some modifications, mostly for my style of programming:

test_session_index.php:
Code:
<?php 
session_start(); 

$_SESSION['user'] = "Hubert"; 

if(isset($_POST['session_check']))
{ 
	header("Location: test_session_index_check.php"); 
} 

echo "(IDX) Session user: " .$_SESSION['user']. "<br>"; 

print '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>Session test</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 

<form action="' . $_SERVER['PHP_SELF'] . '" method="post"> 
<br><br> 
<input type="submit" name="session_check" value="Check Session variables"> 
</form> 

</body> 
</html>';
?>

test_session_index_check.php:
Code:
<?php 
session_start(); 

if(isset($_POST['home']))
{ 
	unset($_SESSION['user']); 
	header("Location: test_session_index.php"); 
} 
echo "(SC) Session user: " .$_SESSION['user']. "<br>"; 

print '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>Session test - Page 2</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 

<form action="' . $_SERVER['PHP_SELF'] . '" method="post"> 
<br><br> 
<input type="submit" name="home" value="Home"> 
</form> 

</body>';
?>

The code works for me. At least $_SESSION['user'] propogates correctly.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks again for the reply.

I copied your code exactly using the same names, etc. I still don't get the session variable crossing the page. I still get a new session id created in the temp file everytime I cross a page (a total of three - page 1 loading, button to page 2 and button back to page 1).

Does your server create three ids or just the one? I suspect that it creates only one - thus giving you access to the session variable it stored on the first id (where mine probably resides but is no longer accessible due to the new ids being created).

So the question begs to be asked: why oh why are there new ids being created each time session_start() is called? What configuration problem do I have and where could it be?

This is SOOOOO frustrating because everything hangs on the *ù$¨%£ù global variable! Deep breaths, 1, 2, 3,...
 
Oh, by the way, I found "Hubert" in the first session ID file and the third one - it makes sense that they be there but not that the extra session files be created.
 
As a test you might try, instead of using "Location:" headers, to have your scripts output links from one script to another.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Now we're cookin' with gas!

I removed the "Location:" script and replaced it with an anchor tag. Clicking on it gets me to the other page AND allows me to use the session variable. Hoorray (and a multitude of thanks - a workaround)!!

BUT, how can I do this without it being an anchor. I need a "goto" statement of some sort in order to keep my buttons. I am not completely fluent in HTML (or PHP for that matter) to know how to do this nicely. Any suggestions?
 
This is what I am going to do...
Code:
<script language="Javascript">
	parent.location = "newPage.htm"
</script>
It replaces the line...
Code:
header("Location: newPage.php");
...which for some reason screws up my session id completely, somehow forcing a new id at every page change.

My solution isn't super pretty, but it works (on Opera anyway, so far). If anyone has another cleaner method please post.

Thanks again spleipnir214
 
I've never seen the "cookie/'Location: header'" problem with Apache before. It's a known issue with Mi[&cent;]ro$oft's web servers, but not Apache.

Have you verified that Apache, and not IIS, is responding to HTTP requests?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
No, I can't say I have. But I have to admit that I don't know how to check. I do know that I load Apache (at bootup through an automatic command line window) and that it is running with the phpdev service (dev4_423) when I run my tests. I can't even tell you if IIS is installed (I'm new at all this). How do I check?
 
The most sure way is to examine the raw HTTP headers sent by your server during a connection. I use telnet for this:[ol][li]Connect to your web server using telnet. From a command prompt, issue:
telnet [your server's IP address] 80[/li][li]Initiate an HTTP request to the web server by hand. Type into your telnet client:
GET / HTTP/1.1[enter]
Host: [ip address of your server][enter]
[enter][/li][/ol]

The server will send you a set of HTTP headers as well as a possible HTML document. One of the headers will be "Server:", which will tell you what web server is responding to port 80 on your machine.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I stuck in a line of code into my little program.

Code:
echo $_SERVER['SERVER_SOFTWARE'];

which gives me:

Code:
Apache/1.3.27 (Win32) PHP/4.2.3

I checked to see if "IIS" was installed on my machine and it is. Should I simply uninstall it? If I do, will uninstalling it affect anything else on my machine? I don't have any IIS experience.

Oh, and by the way, my workaround doesn't work (anymore). I am completely at a loss as to what I could have done to have it screw up now. There have been no modifications to any configuration files that I can recall.

This works (back and forth) with the session id remaining the same:
Code:
<?php
session_start();
echo "(IDX) Session id: " .session_id(). "<br>";
if(isset($_POST['session_check'])) {
?>	<a href = "scCheck.htm">To scCheck</a>
<?php } ?>

Using:
Code:
header("Location: scCheck.htm");
does not.

Nor does:
Code:
if(isset($_POST['session_check'])) {
?>	<script language="Javascript">
		parent.location = "scCheck.htm"
	</script>
<?php
}
?>

It must be some sort of configuration setting that is incorrect somewhere unless the IIS is affecting my Apache test server.
 
Let's take this to the minimum case.

test_session1.php:
Code:
<?php
session_start();

$_SESSION['foo'] = 'bar';

header ('Location: test_session2.php');
?>

test_session2.php:
Code:
<?php
session_start();

print $_SESSION['foo'];
?>

If you create on your server the two files as named above and point your browser to test_session1.php, what happens?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sorry for the tardiness. The server was down the last little while...

Ok, I did ask you suggested.

When I point to the test_session1.php file the browser ends up on the second file - with nothing onscreen.

I looked in the session_id directory and found two session_ids (I had cleared out the old ones prior to the test) - explaining why nothing is printed onscreen.
 
I am using Opera 7.23 but I have IE 6.028 available. B.T.W. IE6 has the same response to the test_session scripts as does Opera.
 
In Opera, go to the menu File/Delete Private data. Then click on the "Manage Cookies" button on the dialog box that opens.

You will see a list of sites you've hit. You can click on your test domain name and Opera will show you the cookies set for that domain.

Does Opera show session cookies for your domain? How many?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top