douhu8851 2014-05-21 03:04
浏览 22
已采纳

PHP Session不起作用

Ok I am trying to create a simple login here but my login code as well as the intropage wont work properly. Tried to tweak the code for SESSION but find no luck.

Here's the code for my login.php:

<?php require_once("includes/connection.php"); ?>
<?php include("includes/header.php"); ?>


<?php

if(isset($_POST["login"])){

if(!empty($_POST['username']) && !empty($_POST['password'])) {
    $username=$_POST['username'];
    $password=$_POST['password'];


    $query=mysql_query("SELECT * FROM usertbl WHERE username='".$username."' AND password='".$password."'");


    $numrows=mysql_num_rows($query);
    if($numrows!=0)
    {
    while($row=mysql_fetch_assoc($query))
    {
    $dbusername=$row['username'];
    $dbpassword=$row['password'];
    }

    if($username == $dbusername && $password == $dbpassword)
    {
    session_start();
    $_SESSION['session_username']=$username;

    /* Redirect browser */
    header("Location: intropage.php");
    }
    } else {
    $message = "Invalid username or password!";
    }

} else {
    $message = "All fields are required!";
}
}
?>




    <div class="container mlogin">
            <div id="login">
    <h1>LOGIN</h1>
<form name="loginform" id="loginform" action="" method="POST">
    <p>
        <label for="user_login">Username<br />
        <input type="text" name="username" id="username" class="input" value="" size="20" /></label>
    </p>
    <p>
        <label for="user_pass">Password<br />
        <input type="password" name="password" id="password" class="input" value="" size="20" /></label>
    </p>
        <p class="submit">
        <input type="submit" name="login" class="button" value="Log In" />
    </p>
        <p class="regtext">No account yet? <a href="register.php" >Register Here</a>!</p>
</form>

    </div>

    </div>


    <?php include("includes/footer.php"); ?>
    <?php if (!empty($message)) {echo "<p class=\"error\">" . "MESSAGE: ". $message . "</p>";} ?>

Then for here's the code for my intropage.php where in I redirect the page.

<?php 
session_start();
if(!isset($_SESSION["session_username"])){
    header("location:login.php");
} else {
?>


<?php include("includes/header.php"); ?>

    <h2>Welcome, <?php echo $_SESSION['session_username'];?>! </h2>
    <p><a href="logout.php">Logout</a> Here!</p>




<?php
}
?>

Any help please? Just wanna make this work or if anything you can tweak so that I can find where I made a mistake. A big thanks!

  • 写回答

4条回答 默认 最新

  • dongmu6225 2014-05-21 04:22
    关注

    You need to check if the session name is set inside all pages using if(isset($_SESSION["session_username"]))

    login.php

    <?php
    
    error_reporting(E_ALL); ini_set('display_errors', 1);
    session_start();
    
    ?>
    
    <?php require_once("includes/connection.php"); ?>
    <?php include("includes/header.php"); ?>
    
    <?php
    
    if(isset($_SESSION["session_username"])){
    // echo "Session is set"; // for testing purposes
    header("Location: intropage.php");
    }
    
    else{
    echo "You are not logged in.";
    }
    
    if(isset($_POST["login"])){
    
    if(!empty($_POST['username']) && !empty($_POST['password'])) {
        $username=$_POST['username'];
        $password=$_POST['password'];
    
        $query =mysql_query("SELECT * FROM usertbl WHERE username='".$username."' AND password='".$password."'");
    
        $numrows=mysql_num_rows($query);
        if($numrows!=0)
    
        {
        while($row=mysql_fetch_assoc($query))
        {
        $dbusername=$row['username'];
        $dbpassword=$row['password'];
        }
    
        if($username == $dbusername && $password == $dbpassword)
    
        {
    
    // old placement
    //    session_start();
        $_SESSION['session_username']=$username;
    
        /* Redirect browser */
        header("Location: intropage.php");
        }
        } else {
    //    $message = "Invalid username or password!";
    
    echo  "Invalid username or password!";
        }
    
    } else {
        $message = "All fields are required!";
    }
    }
    ?>
    
    
    
    
        <div class="container mlogin">
                <div id="login">
        <h1>LOGIN</h1>
    <form name="loginform" id="loginform" action="" method="POST">
        <p>
            <label for="user_login">Username<br />
            <input type="text" name="username" id="username" class="input" value="" size="20" /></label>
        </p>
        <p>
            <label for="user_pass">Password<br />
            <input type="password" name="password" id="password" class="input" value="" size="20" /></label>
        </p>
            <p class="submit">
            <input type="submit" name="login" class="button" value="Log In" />
        </p>
            <p class="regtext">No account yet? <a href="register.php" >Register Here</a>!</p>
    </form>
    
        </div>
    
        </div>
    

    Footnotes:

    Your present code is open to SQL injection. Use prepared statements, or PDO.

    mysql_* functions deprecation notice:

    http://www.php.net/manual/en/intro.mysql.php

    This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

    These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

    Documentation for MySQL can be found at » http://dev.mysql.com/doc/.


    Passwords

    I noticed that you may be storing passwords in plain text. This is not recommended.

    Use one of the following:

    Other links:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作