PHP会话问题

时间:2013-05-03 04:37:20

标签: php

我正在使用会话来保存用户在表单中输入的内容以及他们输入的内容将显示在其他页面上。

它工作得非常好但是在完成所有服务器上传之后,我的代码完全在我身上做了一件事而且我迷路了。

有人能看出他们是否能发现错误吗?我需要新鲜的眼睛。

HTML

    <div id="form"><!--Form Start-->

<form action="home.php" method="post">

    <p>Enter Name <input type="text" id="full_name" name="fullName"    class="name_input"/>

<input type="submit" name="submit"  value="Submit" />

</p>

    </form>

    </div><!--Form end-->

PHP。

    <?php
session_start(); // declaring the variable

if(isset($_POST['fullName'])){ //setting the variable 

    if(empty($_POST['fullName'])){ //if empty dont to nothing but my wep page will reload

    }else{ //if they have do this

        $_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)


        header("Location: home.php"); //then will direct the user to the home page (will also display name on each page)
    }}
    ?>

其他页面上的会话

                  <div id="echo"> <!-- div ECHO start -->           
                                  <?php


                   echo $_SESSION['fullName']
             ?>                  
            </div>  <!--div ECHO end -->    

5 个答案:

答案 0 :(得分:3)

$_SESSION['fullName'] = $_POST['fullName'];
session_register(fullName);

替换为此代码尝试

答案 1 :(得分:0)

您还需要在其他页面上启动会话,并停止脚本设置该会话。标题位置后,您需要在此处使用exit

 <?php session_start();?>
  <div id="echo"> <!-- div ECHO start -->           
  <?php
     echo $_SESSION['fullName'];
   ?>      

您需要在标题位置后使用退出: -

 header('location: home.php');
 exit;         

答案 2 :(得分:0)

您需要在重定向到应该显示数据的页面上添加session_start()。

此外,(我假设您已经意识到)您发布的内容没有任何可以输出数据的内容,例如:

<input type="text" name="fullName" value="<?php echo $_SESSION['fullName']; ?>"/>

答案 3 :(得分:0)

您正在将值“发布”到home.php,这样做无法在原点中设置$_SESSION['fullName'] = $_POST['fullName']

更改

<form action="home.php" method="post">

<form action="name_of_the_first_script.php" method="post">

$_POST['fullName']在重定向之前不存在。

以下是所有内容的样子(以免调用页面index.php):

<div id="form"><!--Form Start-->
    <form action="index.php" method="post">
        <p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>
            <input type="submit" name="submit"  value="Submit" />
        </p>
    </form>
</div><!--Form end-->

现在点击提交后,index.php会被反应,此时$_POST请求意味着条件

if(isset($_POST['fullName'])){

将为true并且可以执行PHP代码,设置$_SESSION变量并将您重定向到home.php,您现在可以阅读先前在$_SESSION中设置的index.php

希望现在能让我更清楚了! :)

答案 4 :(得分:0)

只需将div id格式更改为other,因为它具有默认值并删除空函数,因为您添加了isset functon。

使用此功能。

<div id="myform"><!--Form Start-->

<form action="home.php" method="post">

<p>Enter Name <input type="text" id="full_name" name="fullName" class="name_input"/>

<input type="submit" name="submit" value="Submit" />

</p>

</form>

</div><!--Form end-->

PHP。

<?php
session_start(); 

if(isset($_POST['fullName']))
{ 

    $_SESSION['fullName'] = $_POST['fullName']; //get the session for the name (From the from)

    header("Location: home.php");
    exit(); 

}
?>

其他页面上的会话。

<div id="echo"> <!-- div ECHO start -->           
<?php
 session_start();
 print_r($_SESSION);
 echo $_SESSION['fullName'];

?>                  
</div>  <!--div ECHO end --> 

可能对你有帮助。如果有任何问题,请告诉我。

相关问题