Jump to content

Session basics....


BarneyJoe

Recommended Posts

I'm just having a look at sessions from scratch to see if I can use them to pass data from one page to another.

First I created two basic pages - session_form and session_results :

session_form :

[code]<form action="sessionresults.php" method="POST">
ID <input type="text" name="Photo_ID" />
Title <input type="text" name="Title" />
<input type="submit" />
</form>[/code]

session_results :

[code]<?php
session_start ();
$_SESSION['Photo_ID']=$_POST['Photo_ID'];
$_SESSION['Title']=$_POST['Title'];
?>

<?php echo $_SESSION['Photo_ID']; ?>
<?php echo $_SESSION['Title']; ?>[/code]

This works fine and displays the entered data.

I then tried changing the form action in the first page to session_middle, and created a middle page to test if I could get it to pass through to the results page :

[code]<?php
session_start ();
$_SESSION['Photo_ID']=$_POST['Photo_ID'];
$_SESSION['Title']=$_POST['Title'];
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php echo $_SESSION['Photo_ID']; ?>
<?php echo $_SESSION['Title']; ?><br>
<a href="sessionresults.php">goto next section</a>
</body>
</html>[/code]

This however is drawing a blank - what am I missing here?

Iain

Link to comment
https://forums.phpfreaks.com/topic/28976-session-basics/
Share on other sites

Do you still have these two lines in the last page:
[code]<?php
$_SESSION['Photo_ID']=$_POST['Photo_ID'];
$_SESSION['Title']=$_POST['Title'];
?>[/code]

If so, that would explain the problem. The POST variables wouldn't exist on the third page and you would be setting the session variables to null.
Link to comment
https://forums.phpfreaks.com/topic/28976-session-basics/#findComment-132763
Share on other sites

Thanks -

so its :

[code]<?php
session_start ();
$_SESSION['Photo_ID']=$_POST['Photo_ID'];
$_SESSION['Title']=$_POST['Title'];
etc
?>[/code]

In the first page to start the session, and then

[code]<?php session_start ();?>

+

<?php echo $_SESSION['Photo_ID']; ?>
<?php echo $_SESSION['Title']; ?>
etc
[/code]

In any subsequent page?

Iain



Link to comment
https://forums.phpfreaks.com/topic/28976-session-basics/#findComment-132851
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.