2015-02-11 56 views
1

我想通过一个变量从(register.php)到(submit.php),最后到(yes.php) - 请注意,我已经包括'session_start ();”在每个页面中作为主要PHP页面上的“包含”。PHP会话没有传递到不同的页面(基本)

register.php - 1

<div id = "content"> 
<form action="submit.php" method="post"> 
     <ul> 
      <li> 
       Username*:<br> 
       <input type="text" name="username"> 
      </li> 
      <br> 
      <li> 
       Email Address*:<br> 
       <input type="text" name="email"> 
      </li> 
      <br> 
      <li> 
       Telephone Number*:<br> 
       <input type="text" name="telephone"> 
      </li> 
      <br> 
      <li> 
       Telephone Again*: <br> 
       <input type="text" name="telephoneagain"> 
       <br> 
       </li> 
      <li> 
       <input type="submit" name="submit"> 

      </li> 
     </ul> 
    </form> 
</header> 

这是一个非常基本的登记表。 然后它被发送到

submit.php - 2

<?php 
<div id = "content"> 
<div id = "feature"> 
Your username is: <?php echo $_POST['username']; ?> <br> 
Your email address is: <?php echo $_POST['email']; ?> <br> 
Your telephone number is: <?php echo $_POST['telephone']; ?> <br> 
Just to confirm your telephone number: <?php echo $_POST['telephoneagain']; ?> 
<br> 
<br> 
</div> 
<div id = "validation"> 
Is this correct? <br> 
<li> <a href="yes.php"> Yes </a> </li> 
<li> <a href="register.php"> No </a> </li> 
<?php 
$_SESSION['username'] = $username; 
$_SESSION['email'] = $email; 
$_SESSION['telephone'] = $telephone; 
$_SESSION['telephoneagain'] = $telephoneagain; 
?> 
</div> 
</header> 

同样相当基础,忽视缺乏验证。所以在这里,我可以通过register.php输入用户名。然后,我点击“是”表示信息是正确的。

yes.php - 3

<?php 
$_POST['username']; 
?> 
<?php 
if(isset($_SESSION['username'])) 
    echo $_SESSION['username']; 
?> 
<?php 
$sql = "INSERT INTO users (username, email, telephone, telephoneagain) 
VALUES ('$username', '$email', '$telephone', '$telephoneagain')"; 
?> 

这里的问题是,变量没有从以前的页面传递,因此该页面无法找到他们。

人都在问我有没有在session_start()的最后一页上,这里是完整的代码

这是yes.php - 3

<?php 
    include 'includes/yes/yeshead.php'; 
      include 'includes/yes/yesheader.php'; 
      include 'includes/yes/yescontent.php'; 
      include 'includes/yes/yesnav.php'; 
      include 'includes/yes/yesfooter.php'; 
    include 'core/init.php'; ?> 

的init.php包含在session_start( );

<?php 
session_start(); 
error_reporting(-1); 

require 'core/database/connect.php'; 
require 'core/users.php'; 

$current_file = explode('/', $_SERVER['SCRIPT_NAME']); 
$current_file = end($current_file); 
$errors = array(); 
?> 
+0

你确定包含'session_start()'的文件?如果启动会话的文件需要其余的功能,则应该考虑使用require()而不是include()。 – Abuh 2015-02-11 13:09:17

+0

写入'$ _SESSION'之后,尝试调用'session_write_close()'... – 2015-02-11 13:11:24

+0

您是否启用了cookie?没有Cookie =没有会话 - 除非您手动传递会话ID。 – udondan 2015-02-11 13:15:08

回答

1

亚历克斯帮助我解决了这个问题。我会给他回答点,但他发表评论,所以这将足够

0

直升机,尝试在submit.php:

$_SESSION['username'] = $_POST['username']; 
$_SESSION['email'] = $_POST['email']; 
$_SESSION['telephone'] = $_POST['telephone']; 
$_SESSION['telephoneagain'] =$_POST['telephoneagain']; 

和yes.php:

$username = $_POST['username']; 
$email = $_POST['email']; 
$telephone = $_POST['telephone']; 
$telephoneagain =$_POST['telephoneagain']; 
+0

这是有道理的。 **除非** OP正在使用'register_globals'这是坏BAD坏... – 2015-02-11 13:13:39

+0

:D哈哈很高兴听到 – 2015-02-11 13:16:22

0

前人有关会议的代码,使用session_start();

您正在使用与会话相关的文件,之后包含一个包含session_start();的文件。首先包含包含session_start();的文件。

在yes.php所以用这个:

<?php 
    include 'core/init.php'; 
    include 'includes/yes/yeshead.php'; 
      include 'includes/yes/yesheader.php'; 
      include 'includes/yes/yescontent.php'; 
      include 'includes/yes/yesnav.php'; 
      include 'includes/yes/yesfooter.php'; 
    ?> 

因此,请确保您在使用会话的每个代码添加session_start();,否则将无法正常工作。

+0

我试过输入session_start();在'yes.php'中手动查看是否导致了错误,但是PHP在session_start();已经包含在init.php中,只是忽略它,仍然给我提供了不确定的索引错误 – 2015-02-11 13:23:07

+0

未定义的索引基本上意味着该变量不存在。 – Sombie 2015-02-11 13:24:43

+0

我看到它存在于'submit.php'中,因为它们出现的信息是回声,所以它必须在'submit.php'和'yes.php'之间 – 2015-02-11 13:25:46