简体   繁体   English

使用WAMP Server的PHP会话

[英]PHP Session using WAMP Server

I'm trying PHP session where the user will input an Email in page 1 . 我正在尝试PHP会话,用户将在第1页中输入电子邮件。 After they've entered , they'll be redirect to page 2 with the email displayed as the user. 输入后,他们将被重定向到第2页,并以用户身份显示电子邮件。

Here's my page 1 PHP code looks like : 这是我的页面1 PHP代码如下:

<?php
session_start();
$_SESSION['email']= isset($_POST['email']) ;
if(isset($_POST['submit'])){

header('Location: http://localhost/page2');
}   
?>
<form method="post" action=""> 

Email: <input type="text" name="email"  value=""/> <br/><br/>
<input type="submit" name="submit"/> </form>

Page 2 : 第2页 :

<?php
session_start();
$email = (isset($_SESSION['email']));
echo $_SESSION['email'];
//print_r($_SESSION);
//var_dump($email);
?>

Problem: 问题:

The problem is , when I entered any random emails , it shows number 1 in page 2 when I echoed it. 问题是,当我输入任何随机电子邮件时,当我回显它时,它会在第2页中显示数字1。

That' because you assign the return value of the function isset() ! 那是因为您分配了函数isset()的返回值!

So this should work for you: 因此,这应该为您工作:

if(isset($_POST['submit']) && !empty($_POST['email'])){
    $_SESSION['email']= $_POST['email'];
    header('Location: http://localhost/page2');
}   

Also on your second page you assign it again which you don't have to! 同样在第二页上,您不必重新分配它! You have to check it like this: 您必须像这样检查它:

if (isset($_SESSION['email']))
    echo $_SESSION['email'];

(BTW: I hope this is more an example: header('Location: http://localhost/page2'); because you would have to add the file extension like: header('Location: http://localhost/page2.php'); ) (顺便说一句:我希望这是一个更多示例: header('Location: http://localhost/page2');因为您必须添加文件扩展名,例如: header('Location: http://localhost/page2.php');

The statement below assign TRUE or FALSE to the session variable as isset() returns boolean values not the string passed. 下面的语句将true或FALSE分配给会话变量,因为isset()返回布尔值,而不是传递的字符串。

But, you need to assign the value to it. 但是,您需要为其分配值。

Simply change: 只需更改:

$_SESSION['email']= isset($_POST['email']);

To

if (isset($_POST['email'])) {
  $_SESSION['email']= $_POST['email'];
}

On Page 1 在第1页

<?php
session_start();
if(isset($_POST['submit'])){
    header('Location: http://localhost/page2');
}   
?>

<form method="post" action="http://localhost/page2"> 
Email: <input type="text" name="email"  value=""/> <br/><br/>
<input type="submit" name="submit"/> 
</form>

On Page 2 在第2页

<?php
session_start();
$_SESSION['email'] = $_POST['emai'];
echo $_SESSION['email'];
?>

Page 1 第1页

<?php
session_start();
if(isset($_POST['submit'])) {
$_SESSION['email'] = $_POST['email'];
header('Location: http://localhost/page2');
}   
?>

<form method="post" action=""> 
Email: <input type="text" name="email"  value=""/> <br/><br/>
<input type="submit" name="submit"/> 
</form>

Page 2 第2页

<?php
session_start();

//check if session exist
if(isset($_SESSION['email'])) {
echo $_SESSION['email'];
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM