not understanding why this code is not working
im new to sessions but from what i see it complicated to apply <input>
with them. can you please look at this code and tell me why its not working. i had it working earlier then it died on me. the function of the program is to fill out a form and have it verified for legit information using regular expressions, i just need help with getting the sessions to save the data.
<?php session_start(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>regex</开发者_运维技巧title>
</head>
<body>
<?php
$fname = $_REQUEST['fname'];
$fname = $_SESSION['fname'];
print<<<form
<form method="post" action="">
<input type ="text"
name="fname"
value="">
<input type ="submit">
</form>
form;
$_SESSION['fname'] = $fname;
print $_SESSION['fname'];
?>
</body>
</html>
You are reading $fname from $_REQUEST, then overwriting it with the value from $_SESSION, then putting it back to $_SESSION. So far, it should work as designed :) What are you trying to do? If you want to set the $_SESSION variable with the value received through $_REQUEST, leave out the second "$fname=" line.
Timothy, Change your code so that it checks if the session/request is empty or not
Something like:
if(isset($_REQUEST['fname'])){
$fname = $_REQUEST['fname'];
}else if(isset($_SESSION['fname'])){
$fname = $_SESSION['fname'];
}
Try this:
if (!isset($_SESSION['fname'])) {
$_SESSION['fname'] = ''; // default value
}
if (isset($_POST['fname'])) {
$_SESSION['fname'] = $_POST['fname'];
}
print<<<form
…
form;
print $_SESSION['fname'];
精彩评论