Originally posted by loll
- I am having some problems with my site in regards to session id
- will that data always be lost when using a lheader redirect?
I have a set of 4 pages,
that try to DEMO how to use
<form ...>, POST, and SESSION and let them work together.
<?php
// home.php
session_start();
$user='Guest';
if(isset($_SESSION['name']) && !empty($_SESSION['name'])){
$user=$_SESSION['name'];
echo "Welcome home <b>$user</b>";
}
else
echo "Welcome <b>$user</b>";
if($user=='Guest')
echo "<br><br><a href='page1.php'>Join this website</a>";
else
echo "<br><br><a href='page1.php'>Change my profile</a>";
?>
<!-- //////////////////////////////-->
<?php
// page1.php
// session hasto be started when store or read
session_start();
if ( !isset($_SESSION['name']) || $_POST['reset'] ) $_SESSION['name'] = '';
if ( !isset($_SESSION['pass']) || $_POST['reset'] ) $_SESSION['pass'] = '';
if ($_POST['submit']) {
$usern = trim($_POST['username']);
$passw = trim($_POST['password']);
if ($usern=='?') $usern='';
if ($passw=='?') $passw='';
if(!empty($usern)) $_SESSION['name'] = $usern;
if(!empty($passw)) $_SESSION['pass'] = $passw;
}
$usern = '?';
$passw = '?';
if(!empty($_SESSION['name'])) $usern = $_SESSION['name'];
if(!empty($_SESSION['pass'])) $passw = $_SESSION['pass'];
?>
<!-- when action empty, goes back this page again -->
<form action='' method='post'>
<input type='text' name='username' value='<?php echo $usern ?>'> Username
<br>
<input type='text' name='password' value='<?php echo $passw ?>'> Password
<br>
<input type='submit' name='submit' value='SUBMIT'>
<input type='submit' name='reset' value='RESET'>
<br><br>
<a href='home.php'>Cancel</a>
<br>
<a href='page2.php'>Continue</a>
<!-- //////////////////////////////-->
<?php
// page2.php
// hasto start SESSION
session_start();
$uname = $_SESSION['name'];
$upass = $_SESSION['pass'];
echo "User Name: $uname";
echo '<br>';
echo "Pass Word: $upass";
?>
<br><br>
<a href='page1.php'>Back</a>
<br>
<a href='home.php'>Cancel</a>
<br>
<a href='page3.php'>Continue</a>
<!-- //////////////////////////////-->
<?php
// page3.php
echo "This is";
echo '<br>';
echo "The End";
?>
<br><br>
<a href='home.php'>Home</a>
This is my little set of 4 pages.
It shows how to deal with FORM and SESSION.
Page where most of it happens, is 'page1.php'
It is a simple script, but THE ACTION IS NOT as simple as it seems.
Maybe you can learn something new, from trying this script?
Something you can use in your scripts ...
Also you can have this is a start, and build on to, and modify it.
Regards
/halojoy 2005 sweden