Isset功能不能正常工作


isset function not working properly

页面无法看到代码写在isset函数语句下的登录表单。我已经正确地编写了代码并执行了很多次,但是现在isset语句内编写的代码不起作用了。代码如下:-

<?php
session_start();
echo "<p style='"font-color: #ff0000;'"> Catogoies </p>";
echo '<link href="var/www/html/sample.css" rel="stylesheet">';

require_once('../html/conn.php');
$query = "select * from catogories";
mysqli_select_db($dbc, 'odit');
$retrieve = mysqli_query($dbc, $query);
if(!$retrieve)
{
    die(mysqli_error($query));
}
while($row=mysqli_fetch_array($retrieve, MYSQL_ASSOC)){
echo "<p style='"font-color: #ff0000;'">".'<a href="cats.php?        catogory='.urldecode($row["Name"]).'">'.$row["Name"].'</a>'."</p>";
$_SESSION['cat']=$row["Name"]; 
}
if(!($_SESSION)) {
    session_start();
}if(isset($_SESSION['lgout']))//the variable logout intialization line
{
if($_SESSION['lgout']!=1||$_SESSION['signup']){
echo "Hello : ".'<a href = "profile.php">'.$_SESSION['unme'].'</a>';    echo "<br><br>";
echo '<a href="logout.php">'."Logout";}
else { 
include 'lform.php'; echo "<br><br>";   
echo '<a href="Sign_up.php">'."Sign up"."<br>";
} }
mysqli_close($dbc);
//include 'lform.php';
?>
<br>
<a href = 'adding_catogory.php'>Create a New Catogory</a><br><br>
<a href = 'Log_in.php'></a>
<?php
$db = @mysqli_connect("localhost", "oddittor", "Odit@123", "odit");
if(isset($_POST['login'])){
$username=mysqli_real_escape_string($db, $_POST['l_id']);
$password=mysqli_real_escape_string($db, $_POST['pswd']);
$sql="SELECT * from users where usrName='$username' and pswrd =    '$password'"; 
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
$count=mysqli_num_rows($result) or die(mysqli_error($db));
if($count>0) {
    $_SESSION['unme']=$username; //This is the global session variable...used for storing the variables across the pages.
    $_SESSION['lgout']=0;
    header('Location : session.php'.$_SESSION['unme']);
    header("Location : Homepage.php".$_SESSION['unme'].$_SESSION['lgout']); header( "refresh:0;url=Homepage.php" );
    $_SESSION['unme']=$username;
}
else {
    $error = "Invalid Details! Please Renter them"; }
}
?>

问题在

if(isset($_SESSION['lgout']))

行,如果我删除这行,我可以看到登录页面表单,但通过这样做,我得到未定义的变量登出的错误,每当我第一次打开页面。

这是注销脚本

<html>
<?php
session_start();
$_SESSION['lgout']=1;
$_SESSION['signup']=0;
echo ' You have been successfully logged out';
header('Location : Homepage.php'.$_SESSION['lgout']);header(     "refresh:0;url=Homepage.php" );
?>
</html>

你需要把

session_start();

全局设置在页面开头。因为它不能得到$_SESSION对象

直接删除

session_destroy();

可以访问所有的$_SESSION值。

您的查询不安全。使用Prepared Statements代替所有查询。http://php.net/manual/en/mysqli.quickstart.prepared-statements.php