views:

92

answers:

4

i cant seen to get the php sessions to work, im trying to create a form that saves the data then put its against regular expressions to verify its legit information. at the moment im trying to figure out how to get the sessions to register but i cant figure it out, please help. im new to sessions

<?php session_start(); ?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<title>regex</title>


</head>

<body>

<?php

$_SESSION['fname']=$fname;

$ufname = $_SESSION['fname'];

print<<<form
 <form method="post" action="">
form;

print<<<firstname
First Name:
 <input type="text"
           name="fname"
     value="$fname">

firstname;

print<<<submit

 <input type="submit">

submit;
print "</form>";


if($fname == NULL){
print "lets go";
}else{
print "good";
}// end if 

print "$ufname";

$_SESSION['ufname'] = $ufname;
?>

</body>
</html>
A: 

You need to call session_start() before you can access $_SESSION. Furthermore, you're writing to $_SESSION['fname'] before you read from it.

EDIT: The updated version of your code shows you're calling session_start(), but also that you're indeed overwriting the value of $_SESSION['fname'] with $fname (which is undefined at this point) before you read $ufname from it. Try to change the order of those rows, that probably should fix the problem.

Kaivosukeltaja
he has a session_start right at the start
thephpdeveloper
session_start() is at the top (I missed it too) and he's relying on register globals for the $fname value.
cletus
The previous, unformatted version which I answered to didn't have session_start(). Thanks for the downvotes, guys.
Kaivosukeltaja
+1 @Kaivosukeltaja spotted the bug right away. It's `$_SESSION['fname']=$fname;` that's causing the session data to be destroyed, since $fname isn't set yet.
wimvds
+3  A: 

There aren't any problems with the way you're handling the session. However, you're setting $_SESSION['fname'] to $fname which is never previously declared or set. Thus, the value stored in the $_SESSION['fname'] index is going to be null. That's why you're not getting any output from the session variable.

BraedenP
$fname is set if register globals is on.
cletus
Ah, right... Okay.
BraedenP
+2  A: 

Your problem is that you're accessing $fname directly. Try to use $_POST['fname']. Your way of using the variable has security risks and this is the reason it is disabled by the default in newer versions

rossoft
A: 
<?php session_start(); ?> 

<html> 
<head></head> 
<body> 

<?php 
if (!isset($_SESSION['name']) && !isset($_POST['name'])) { 
    // if no data, print the form 
?> 
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
        <input type="text" name="name"> 
        <input type="submit" name="submit" value="Enter your name"> 
    </form> 
<?php 
} 
else if (!isset($_SESSION['name']) && isset($_POST['name'])) { 
    // if a session does not exist but the form has been submitted 
    // check to see if the form has all required values 
    // create a new session 
    if (!empty($_POST['name'])) { 
        $_SESSION['name'] = $_POST['name']; 
        $_SESSION['start'] = time(); 
        echo "Welcome, " . $_POST['name'] . ". A new session has been activated for you. Click <a href=" . $_SERVER['PHP_SELF'] . ">here</a> to refresh the page.";
        echo "<br>Your session id is: ".session_id(); 
    } 
    else { 
        echo "ERROR: Please enter your name!"; 
    } 
} 
else if (isset($_SESSION['name'])) { 
    // if a previous session exists 
    // calculate elapsed time since session start and now 
    echo "Welcome back, " . $_SESSION['name'] . ". This session was activated " . round((time() - $_SESSION['start']) / 1) . " seconds(s) ago. Click <a href=" . $_SERVER['PHP_SELF'] . ">here</a> to refresh the page."; 
    echo "<br>Your session id is: ".session_id();
} 
?> 
</body> 
</html>
Newb