dsx666666 2012-02-10 19:48
浏览 19
已采纳

基于会话的登录无法正常工作

I am currently working on a site and i'm making a login system. I am using sessions to keep track of login status. The pages involving login until now are shown below: (I change pages defining post variables in the url, like this: site.domain.com/?home)

index.php

/* CONNECTION TO CHECK LOGIN STATUS */
<?php
if (isset($_GET["sair"])){
    session_start();
    session_destroy();
}
if (isset($_SESSION["user"]) && isset($_GET["entrar"])){
    mysql_connect("localhost","dbusr","password") or die("Can't connect to DB");
    mysql_select_db("mydb") or die("Can't select DB");
    $userinfo = $_SESSION["uinfo"];
}
?> /* START OF THE PAGE, RANDOM UNIMPORTANT HTML */
<?php if (!isset($_SESSION["user"])) { ?> /*NOT LOGGED IN...*/
<form id="loginform" method="post" action="scripts/checklogin.php">
<h2>Login</h2>
<?php if (isset($_GET["falha"])) { echo "<span class='erro'>Nome ou senha incorretos</span>"; }?> /*IF LOGIN ERROR*/
<input type="text" name="user" autofocus placeholder="Apelido"/><br />
<input type="password" name="pass" placeholder="Senha"/><br /> /*LOGIN FORM*/
<input type="submit" value="Entrar"/>
</form>
<?php
} else { /*LOGGED IN...*/
if ($userinfo["sexo"]=="0"){ /*GENDER*/
echo "Bem-vindo, ".$userinfo["nome"];
} else {
echo "Bem-vinda, ".$userinfo["nome"];
}?>
<?php }?>

scripts/checklogin.php

<?php

mysql_connect("localhost","dbusr","password") or die("Can't connect to DB");
mysql_select_db("mydb") or die("Can't select DB");
$user = mysql_real_escape_string(stripslashes($_POST["user"]));
$pass = md5(mysql_real_escape_string(stripslashes($_POST["pass"])));
$result = mysql_query("SELECT * FROM users WHERE apelido = \"".$user."\" AND senha = \"".$pass."\"");
if (count($result)==1) {
    session_start();
    $_SESSION["user"] = $user;
    $_SESSION["pass"] = $pass;
    while ($row = mysql_fetch_assoc($result)) {
        $_SESSION["uinfo"] = $row;
    }
    header("location:../?entrar");
} else {
    header("location:../?falha");
}
?>

But when i go to the page and enter my info, it still doesn'tshow the logged in part. Also, when i input a incorrect login, it does not show me the text over the login form (the <span>)

  • 写回答

1条回答 默认 最新

  • dougu7546 2012-02-10 19:57
    关注

    You need to add session_start() in index.php before checking $_SESSION["user"].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改