isset功能不正常

时间:2016-09-14 06:21:17

标签: php session-variables isset

页面无法看到其代码在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']))

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

这是注销脚本

<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>

2 个答案:

答案 0 :(得分:4)

你需要把你的

session_start();

全局在页面开头。因为它无法获得$_SESSION对象。

答案 1 :(得分:1)

只需删除

session_destroy();

您可以访问所有$_SESSION值。

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

相关问题