我不确定为什么这不起作用,但我在下面的代码中想说的是,如果用户没有登录if ((!isset($username)) && (!isset($userid))){
,那么如果他们已登录则查看表单然后显示消息说明他们必须退出才能查看该页面。
问题是,即使用户已登录,它仍会显示表单而不是消息。
以下是代码:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
session_start();
include('member.php');
$user = (isset($_POST['user'])) ? $_POST['user'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
if ((!isset($username)) && (!isset($userid))){
echo "<form action='./forgotpass.php' method='post'>
<table>
<tr>
<td></td>
<td id='errormsg'>$errormsg</td>
</tr>
<tr>
<td>Username</td>
<td><input type='text' name='user' /></td>
</tr>
<tr>
<td>Email</td>
<td><input type='text' name='email' /></td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='resetbtn' value='Reset Password' /></td>
</tr>
</table>
</form>";
}
else
{
echo "Please Logout to view this Page.";
}
?>
在上面的代码中,我包含了member.php页面,我不知道这是否有很大的不同,但是member.php页面的代码如下:
<?php
if (isset($_SESSION['teacherforename'])) {
$_SESSION['teacherforename'] = $_SESSION['teacherforename'];
}
if (isset($_SESSION['teachersurname'])) {
$_SESSION['teachersurname'] = $_SESSION['teachersurname'];
}
if (isset($_SESSION['teacherid'])) {
$userid = $_SESSION['teacherid'];
}
if (isset($_SESSION['teacherusername'])) {
$username = $_SESSION['teacherusername'];
}
?>
答案 0 :(得分:1)
检查它是否已设置且是否具有任何值。
if ( (!isset($username) && empty($username)) && (!isset($userid) && empty($userid)) ){
答案 1 :(得分:1)
<?php
$user = (isset($_POST['user'])) ? $_POST['user'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
if ((!isset($username)) && (!isset($userid))){
echo 'empty set';
}
以上将输出空集,表示$ user和$ email设置
要使其正确,也请使用empty()
纠正一个
$user = (isset($_POST['user'])) ? $_POST['user'] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
if ((!isset($username)) && !empty($username) && (!isset($userid) && !empty($email ))){
echo 'empty set';
}
答案 2 :(得分:0)
$user = (isset($_POST['user'])) ? $_POST['user'] : NULL;
$email = (isset($_POST['email'])) ? $_POST['email'] : NULL;
我认为将变量设置为''将返回true isset()
isset()
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.