Php用户登录系统

时间:2014-02-04 08:29:58

标签: php mysql session login

到目前为止,我已成功注册成员,但我无法使用其中任何一个登录。它不会在标题上写入用户名。即使我已登录,也只是说“欢迎客人”。我不知道问题是否是会话。

的login.php

session_start();
include ("connection.php");

if(isset($_POST["username"], $_POST["pw"])){

$username = mysql_real_escape_string($_POST["username"]);
$pw = mysql_real_escape_string(md5($_POST["pw"]));

$sql = mysql_query("SELECT * FROM uye WHERE kullanici_adi = '$username' AND sifre = '$pw'");

if(mysql_num_rows($sql) == 1){

    $_SESSION["loggedIn"] = 1;
    $_SESSION["username"] = $username;
}
else{
    $_SESSION["loggedIn"] = "";
}
}

loginValidation.js

$(document).ready(function(){

var form = $("#loginForm");

$("#loginButton").click(function(){

    form.validate({

       rules:{
           loginUsername:{
               required: true
           },
           loginPassword:{
               required: true
           }
       },
        messages:{
            loginUsername:{
                required: "Lütfen kullanıcı adınızı giriniz."
            },
            loginPassword:{
                required: "Lütfen şifrenizi giriniz."
            }
        },
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
        }
    });

    var username = $("#loginUsername").val();
    var pw = $("#loginPassword").val();

    $.ajax({
        type: "post",
        url: "login.php",
        data: "username=" + username + "&pw=" + pw
    });

    if(form.valid() == true){

        form.submit();
    }
});
});

的header.php

if (isset($_SESSION['loggedIn'], $_SESSION['username']) && $_SESSION['loggedIn'] == 1) {

    session_start();
    echo "Hoşgeldin" . $_SESSION['username'] . "!";
}
else{
    echo "Hoşgeldin Ziyaretçi";
}

2 个答案:

答案 0 :(得分:2)

您必须将session_start();放在 header.php 文件的顶部。

session_start();
if (isset($_SESSION['loggedIn'], $_SESSION['username']) && $_SESSION['loggedIn'] == 1) {
    echo "Hoşgeldin" . $_SESSION['username'] . "!";
}
else{
    echo "Hoşgeldin Ziyaretçi";
}

答案 1 :(得分:0)

看看你的评论你还有问题, 对会话变量执行var_dump以查看它们是否符合您的期望。