Jump to content

in need of help


stuckwithcode

Recommended Posts

my page is called test.php, when i type in the address http://localhost/test.php?test=true why does the code not echo the word hello and then end the session.

 

<?php 
session_start(); 

if ($_GET['test']) 
{
$_SESSION['testsession'] = true;		
header('Location: test.php');
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<?php
if (isset($_SESSION['testsession']))
{
echo "Hello";
unset($_SESSION['testsession']);
}		
?>
</body>
</html>

 

thanks if not possible other ideas welcome

Link to comment
Share on other sites

Because you are redirecting to the same page, but without the ?test=true on the end of the URL, you are only 'seeing' the result of the page being requested the 2nd time.

 

if ($_GET['test']) will be FALSE the 2nd time the page is requested, but because the code on the page continued executing the first time it was requested, $_SESSION['testsession'] was previously unset and the echo code is skipped over when the page is requested the 2nd time.

 

I'm not sure what you are trying to accomplish with the posted code, but a header redirect normally needs an exit/die statement following it to prevent the remainder of the code on the page from being executed while the browser is performing the redirect.

Link to comment
Share on other sites

no, it simply sends another request to the page, not a redirect, but if you don't stop execution with die() or exit(), the rest of the page will be executed (at least until the header request is sent, and the page is reloaded

 

adding a die() or exit() after the header should fix it

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.