我正在尝试存储会话值并检查是否填写了字段,然后将表单函数返回到注册表单。
但是在下面的行中出现错误。
if(($_SESSION['name'] == '') && ($_SESSION['email'] == '') && ($_SESSION['phone'] == ''))
及以下是在表单页面中使用的完整php代码。
<?php
session_start();
if (isset($_POST['signup']))
{
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
}
if(($_SESSION['name'] == '') && ($_SESSION['email'] == '') && ($_SESSION['phone'] == ''))
{
exit();
}
else
{
header('Location : signup-form.php');
exit();
}
?>
答案 0 :(得分:2)
这是最好的一个
<?php
session_start();
if (isset($_POST['signup']))
{
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
}
if(!isset($_SESSION['name'])){ // if name is empty
exit('Type your name.');
}elseif(!isset($_SESSION['email'])){ // if email is empty
exit('Type your email.');
}elseif(!isset($_SESSION['phone'])){ // if phone number is empty
exit('Type your phone.');
}else{ // ohh look, they're all fine
header('Location : signup-form.php');
exit();
}
?>
答案 1 :(得分:1)
使用标题时从代码中删除空格,然后使用isset()
检查该会话是否已设置
<?php
session_start();
if (isset($_POST['signup']))
{
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone'];
}
if(!isset($_SESSION['name']) && !isset($_SESSION['email']) && !isset($_SESSION['phone']))
{
exit();
}
else
{
header('Location : signup-form.php');
exit();
}
?>
答案 2 :(得分:0)
您正在使用 isset()
设置之前检查SESSION正确的方法是:
if(!isset($_SESSION['name']) && !isset($_SESSION['email']) && !isset($_SESSION['phone'])){
exit();
}
else{
//...
}