PHP代码注销

时间:2013-10-21 08:55:15

标签: php web

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

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

3 个答案:

答案 0 :(得分:0)

由于您的代码未在username中设置passwordSESSION。它应检查您的设置,即memID

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

实际上应该是

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

答案 1 :(得分: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'];

您的问题将得到解决。

答案 2 :(得分: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']而不是用户名和密码