php会话的问题

时间:2009-11-05 06:22:26

标签: php variables session

我看不到让PHP会话工作,我试图创建一个保存数据的表单,然后将其与正则表达式进行对比,以验证其合法信息。目前我试图弄清楚如何让会议注册但我无法弄清楚,请帮助。我刚开始参加会议

<?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

$_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>

4 个答案:

答案 0 :(得分:3)

您处理会话的方式没有任何问题。但是,您将$ _SESSION ['fname']设置为$ fname,这是以前从未声明或设置的。因此,存储在$ _SESSION ['fname']索引中的值将为null。这就是为什么你没有从会话变量中获得任何输出。

答案 1 :(得分:2)

您的问题是您正在直接访问$ fname。尝试使用$ _POST ['fname']。您使用该变量的方式存在安全风险,这就是它在较新版本中被默认禁用的原因

答案 2 :(得分:0)

<?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> 

答案 3 :(得分:-1)

您需要先致电session_start(),然后才能访问$_SESSION。此外,在您阅读之前,您正在写信$_SESSION['fname']

编辑:您的代码的更新版本显示您正在调用session_start(),但您确实在使用$ fname(此时未定义)覆盖$_SESSION['fname']的值之前你从中读取了$ ufname。尝试更改这些行的顺序,这可能应该解决问题。