0

每当我登录到我的帐户时,它都应该显示“注销”选项卡,但它仍然显示“登录和注册”选项卡,尽管我的代码有条件。有人请帮忙。非常感谢。:)

<?php

    session_start();    
    if(isset($_SESSION['username']) && isset($_SESSION['password'])){
        $username=$_SESSION['username'];
        $password=$_SESSION['password'];
        }
        ?>


<div class="navigation">
                           <ul>
                    <li><a href="Home.php">Home</a></li>
                    <li><a href= "GALLERY.php">Gallery</a></li>
                    <li><a href="contact.php">Contact Us</a></li>
                    </ul>
                <ul>

                    <?php
                    if(isset($_SESSION['username'])&& isset($_SESSION['password'])){?>
                    <li><a href="?page=LogOut">Log Out</a></li>
                <?php } else { ?>
                <li><a href="?page=index2">Register</a></li>
                <li><a href="?page=index">Log In</a></li>
                    <?php } ?>
                </ul>



</div>

我登录的另一个类。

<html>
<?php
    //Start session
    session_start();    
    //Unset the variables stored in session
    if(isset($_POST['username'])&& isset($_POST['password'])){
    $username=$_POST['username'];
    $password=$_POST['password'];

    $con = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db('store',$con);
    $sql = "SELECT *FROM `members` WHERE username='%s' AND password='%s'";
    $sql = sprintf($sql,$username,$password);
    $result=mysql_query($sql) or die(mysql_error());
    $row=mysql_fetch_assoc($result);

    if(count($row)<=1){
    echo "Incorrect username or Password!".count($row);
    }else{
    $_SESSION['memID']=$row['memID'];
    $_SESSION['fname']=$row['fname'];
    $_SESSION['lname']=$row['lname'];
    $_SESSION['gender']=$row['gender'];
    $_SESSION'username']=$row['username'];
    $_SESSION['password']=$row['password'];
    header("Location:Home.php");
    }
    mysql_close($con);
    }




?>

<body>
<form name="loginform" action="login_exec.php" method="post">
<div id="body"><table width="309" border="0" align="center" cellpadding="2" cellspacing="5">
  <tr>
    <td colspan="2">
        <!--the code bellow is used to display the message of the input validation-->
         <?php
            if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
            echo '<ul class="err">';
            foreach($_SESSION['ERRMSG_ARR'] as $msg) {
                echo '<li>',$msg,'</li>'; 
                }
            echo '</ul>';
            unset($_SESSION['ERRMSG_ARR']);
            }
        ?>
    </td>
  </tr>
  <tr>
    <td width="116"><div align="right">Username</div></td>
    <td width="177"><input name="username" type="text" /></td>
  </tr>
  <tr>
    <td><div align="right">Password</div></td>
    <td><input type="password" class="input" name="password" /></td>
  </tr>
  <tr>
    <td><div align="right"></div></td>
    <td><input name="" type="submit" value="login" /></td>
  </tr>
    <tr>
    <td><div align="left"></div></td>
    <td><a href="index2.php">Register</a></td>
  </tr>
</table>
</div>
</form>
4

3 回答 3

0

由于您的代码未设置usernamepasswordSESSION. 它应该检查您正在设置的内容,那就是memID

if(isset($_SESSION['username'])&& isset($_SESSION['password']))

实际上应该只是

if(isset($_SESSION['memID']))
于 2013-10-21T09:10:42.310 回答
0

您没有将数据存储$_SESSION['username']$_SESSION['password']

在您的登录文件中,您需要这样的东西

$_SESSION['memID']=$row['memID'];
$_SESSION['fname']=$row['fname'];
$_SESSION['lname']=$row['lname'];
$_SESSION['gender']=$row['gender'];
$_SESSION['username']=$username;
$_SESSION['password']=$password; // <<<--- It's not a good idea to store the password....

如果您不想添加此行,那么您应该在您的 html 代码中询问$_SESSION['memID']$_SESSION['fname']而不是用户名和密码

于 2013-10-21T09:24:32.017 回答
0

替换这部分代码

$_SESSION['memID']=$row['memID'];
$_SESSION['fname']=$row['fname'];
$_SESSION['lname']=$row['lname'];
$_SESSION['gender']=$row['gender'];

有了这个

$_SESSION['memID']=$row['memID'];
$_SESSION['fname']=$row['fname'];
$_SESSION['lname']=$row['lname'];
$_SESSION['gender']=$row['gender'];
$_SESSION['username']=$row['username'];
$_SESSION['password']=$row['password'];

您的问题将得到解决。

于 2013-10-21T09:23:40.950 回答