会话在登录页不起作用


Session not work at login page?

我刚刚在我的HP Server中构建了Centos 6.4,我使用了PHP 5.3.3、Apache 2.2.15和Mysql 5.1.69。当我想登录总是失败时,源代码如下:

index.php
    <?
include "functions.php";
start_session();
session_destroy();
start_session();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>Please Login First</title>
    <link href="Tampilan/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="Tampilan/css/validationEngine.jquery.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="Tampilan/js/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="Tampilan/js/jquery.validationEngine.js" ></script>
        <script type="text/javascript" src="Tampilan/js/jquery.validationEngine-en.js" ></script>
        <style type="text/css">
            .myTableStyle {
                position: absolute;
                top: 45%;
                left: 37%;
            }
            #full-screen-background-image {
                z-index: -999;
                width: 100%;
                height: auto;
                position: fixed;
                top: 0;
                left: 0;
            }
            #wrapper {
                position: relative;
                width: 800px;
                min-height: 400px;
                margin: 100px auto;
                color: #333;
            }
            .overlay-text {
                position: absolute;
                top: 20px;
                left: 225px;
                font-size: 38.5px;
                font-weight: bold;
                color: #ddd;
                font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
            }
        </style>
    <script src="content/md5.js"></script>
    <script>
    function cek()
    {
    if(!document.form1.username.value)
        {
        document.form1.username.focus();
        window.alert("Please Fill In The Username");
        }
    else if(!document.form1.password.value)
        {
        document.form1.password.focus();
        window.alert("Please Fill In The Password");
        }
    else {
        document.form2.username.value=document.form1.username.value;
        document.form2.password.value=calcMD5(document.form1.password.value);
        document.form2.submit();
        }   
    }
    </script>
    <link href="style.css" rel="stylesheet" type="text/css">
    <link href="bootstrap.min.css" rel="stylesheet" type="text/css">
    <noscript>
        <div class="pagetitle">This page cannot be loaded properly because your browser has been set to deny the usage of Javascript. Please enable the Javascript support at your browser in order to continue</div>
        <meta http-equiv="Refresh" content="0;noscript.php"/>
    </noscript>
</head>
<div class="all-page">
        <div class="page-header">
            <!--<img alt="image" src="Tampilan/img/Stema1.jpg" style="height: 300px;width: 100%;z-index: -1;"/>!-->&nbsp;
            <p class="overlay-text">
                SIS Admission
            </p>
            <body onload="document.form1.username.focus()">
                <form name="form1" action="javascript:cek()">
                    <table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
                        <tr>
                                <td width="100%" valign="middle" align="center">
                                    <table border="0" align="center" width="300" cellpadding="0" cellspacing="0">
                                        <tr>
                                            <td align="center" colspan="2" height="50"><div class="medTitle">Please Login</div></td>
                                        </tr>
                                        <tr>
                                            <td align="center" width="120" height="30"><div class="smallTitle">Username </div></td>
                                            <td align="left"><input type="text" name="username" class="input"/></td>
                                        </tr>
                                        <tr>
                                            <td align="center" height="30"><div class="smallTitle">Password </div></td>
                                            <td align="left"><input type="password" class="input" name="password"/></td>
                                        </tr>
                                        <tr>
                                            <td align="center" colspan="2" height="40"><input type="submit" class="button" value="Login"/></td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                        </form>
                        <form name="form2" action="login.php" method="post">
                            <input type="hidden" name="username"/>
                            <input type="hidden" name="password"/>
                            <input type="hidden" name="sess" value="<?=session_id();?>"/>
                        </form>
                <div class="well navbar navbar-fixed-bottom">
            <center>
                Copyright &copy; 2013 <a href="http://www.stellamaris.co.id/" target="_blank">Saint John's Catholic School</a> Developed by IT Department Of Saint John's Catholic School<a href="javascript:pergiKe('credit')"></a>. All Rights Reserved.
            </center>
    </div>
</body>
</html>

login.php
<?
include "functions.php";
if(!isset($_POST['username'])||!isset($_POST['password'])||!isset($_POST['sess'])){
?>
<script>
alert("Silahkan masukan username/password anda");
location.href="index.php";
</script>
<?
}
else
{
    session_start();
    $session_id=$_POST['sess'];
    if($session_id!=session_id()){
        ?>
        <script>
        alert("Untuk dapat menggunakan website ini, browser anda haruslah menerima cookie yang diberikan");
        location.href="index.php";
        </script>
        <?
    }
    else{
        $username=$_POST['username'];
        $password=$_POST['password'];
        connect_local();
        $pola_password="^[a-zA-Z0-9]{1,}$";
        $pola_username="^[a-zA-Z0-9_]{1,}$";
        if(!ereg($pola_username,$username)){
            ?>
            <script>
            alert("Silahkan masukan username");
            location.href="index.php";
            </script>
            <?
        }
        else if(!ereg($pola_password,$password)){
            ?>
            <script>
            alert("Silahkan masukan password");
            location.href="index.php";
            </script>
            <?
        }
        else{
            $query="select * from ms_user where username='$username' and password='$password'";
            $result=mysql_query($query);
            if(mysql_num_rows($result)>1) {
                ?>
                <script>
                alert("Username/password tidak terdaftar");
                location.href="index.php";
                </script>
                <?
            }
            else if(mysql_num_rows($result)==1){
                 $hasil=mysql_fetch_array($result);
                 if($hasil['status']==0){
                ?>
                    <script>
                    alert("Username/password tidak terdaftar");
                    location.href="index.php";
                    </script>
                    <?
                 }
                 else{
                     $username=$hasil['username'];
                     $id_ms_user=$hasil['id_ms_user'];
                     $status=$hasil['status'];
                     $_SESSION['ses_username']=$username;
                     $_SESSION['ses_user_id']=$id_ms_user;
                     $_SESSION['ses_access_level']=$status;
                     header("location:main.php");
                 }
            }
            else {
            ?>
                <script>
                alert("Username/password tidak terdaftar");
                location.href="index.php";
                </script>
            <?
            }
        }
    }
}
?>

当我使用PHP 5.1.6、Apache 2.2.3和Mysql 5.0.24a时,我以前没有遇到过这样的问题。

你在用什么代码

start_session();
session_destroy();
start_session();

删除所有这些并用替换

    session_start();
    if($_SESSION['ses_username'] == "")
    {
        session_destroy();
    }

更换

start_session();

由-

session_start();

itssession_start()NOT start_session()或者您可以在index.php中使用

<?
    include "functions.php";
    if(!isset($_SESSION))
    {
        session_start();
    }
    if($_SESSION['ses_username'] == '')
    {
        session_destroy();
    }
?>