Hi,

I am kind of new to php, and have run into a problem. Whenever I try to pass on values from one page to another, using hyperlink, the variables in the session don't carry on. Any ideas as to why? Variables get passed when I use header(location:) code in php.

Can anyone recommend ways that I can pass sensitive information with moderate security from one page to another?

Thank you

P.S.

I included the session_start() on the link page.


Link page

<?php 
session_start();
?>
 
<html>
<body>
<?php echo "User:". $_SESSION['user']; ?>

<a href="Untitled-1.php">HOME</a>
</body>
</html>

Home page

<?php 
session_start();
if(isset($_SESSION['user']))
{}
else
{
	$_SESSION['user'] = "Anonymous";
	//header("location:Untitled-1.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript">
x()
{
	<?php
		session_destroy(); 
	?>

}
</script>
<body>

<?php
echo "User:". $_SESSION['user'];
if ($_SESSION['user'] == "Anonymous")
{
	echo 
	'<form id="form1" name="form1" method="post" action="check.php">
  <input name="myusername" type="text" id="myusername" value="" size="45" />
  <input name="mypassword" type="password" id="mypassword" value="" size="45" />
  <input type="submit" name="button" id="button" value="Submit" />
  </form>';
}
else
{
	echo 
	'<form id="form2" name="form2" method="post" action="">
  <input type="submit" name="button" id="button" value="Logout" onclick="x()" />
  </form>';
}


?>
<a href="linnk1.php">link </a>
</body>
</html>
Member Avatar for diafol

Your problem is the session_destroy() function.
Do this:

x()
{
	<?php
		//session_destroy(); 
	?>
 
}

and you'll see that Anonymous is still logged in.

THe problem is that you're trying to do an AJAX call without the ajax! You can't call a php function from a js function unless it's via ajax.

The session_destroy is run (regardless of the fact that it is within the js function) on page load.

commented: helped a lot +2

Thank you so very much, problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.