douduocuima61392 2014-09-03 00:34
浏览 47
已采纳

我的PHP用户登录无效[关闭]

I have searched for a few days for the answer for this, so I figured I would try to come here to try to learn where I went wrong, rather than scrap the code and rewrite everything.

I have a user registration form and a mysql db with all relevant user login field... Username, name, password (sha1), etc. When I register a user, with the same information from the connectdb.php that connects to my database, it works, and enters all information into the database (with the password encrypted as well).

However... here it comes.. when I try to login with some credentials that I know should match, my code is returning my message from my code below, "This Information does not match any records; please try again."

/////////// Here is the code for my login, truncated:

<?php 
include_once('connectdb.php');
include_once('global.php');
include_once('validate.php'); 
?>

    <form action="?action=userlogin" method="post">
    <input type="text" name="username" placeholder="Enter Your Username" required /><br>
    <input type="password" name="password" placeholder="Password" required /><br>
    <input type="checkbox" name="remember" value="yes" checked="checked" /> Remember Me<br>
    <input type="Submit" value="Login" />



////////// Here is the code for my form validation:

    <?php
    $action = $_REQUEST["action"];
    switch($action){
    /////// START USER LOGIN ACTION /////////
case "userlogin":
if(isset($_POST['username'])) {
    $message = '';
    $username=$_POST['username'];
    $password=$_POST['password'];
    $remember=$_POST['remember'];

// Error handling
if( (!$username) || (!$password) ){
    $message = 'Please fill in both username and password';
    } else {
    //secure the data
    $username = mysql_real_escape_string($username);
    $password = sha1($pass1);
    $query = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1") or die('Could not check member');
    $count=mysql_num_rows($query);
    if($count == 0){
        $message ='This Information does not match any records; please try again.';
    } else {
        //start the sessions
        $_SESSION['password'] = $password;
        while($result = mysql_fetch_array($query)){
        $id = $result['id'];
    }
    $_SESSION['username'] = $username;
    $_SESSION['id'] = $id;

    if($remember == "yes"){
        //create the cookies
        setcookie("id_cookie",$id,time()+60*6*24*90, "/");
        setcookie("pass_cookie",$password,time()+60*6*24*90, "/");
    }       
    // Eventually point this to the member page
    header("location: http://www.example.com/loggedin/");
    $message ='Thank you for logging in!';
    } // End if email and password match records
    } // End if email and password are both filled out
    } // End userlogin case 
    } // End switch statement
    ?>

/////////// Here is my global/sessions

    <?php 
    session_start();
    include_once('connectdb.php');

//checking if the sessions are set
if(isset($_SESSION['username']))  {
$session_username = $_SESSION['username'];
$session_pass = $_SESSION['password'];
$session_id = $_SESSION['id'];

// check if the member exists
$query = mysql_query("SELECT * FROM users WHERE id='$session_id' AND password='$session_pass' LIMIT 1") or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 0){
    // logged in stuff here
    $logged =1;

} else {
    header("Location: http://www.example.com/logout.php");
    exit();
}
} else if(isset($_COOKIE['id_cookie'])) {
$session_id = $_COOKIE['id_cookie'];
$session_pass = $_COOKIE['pass_cookie'];

$query = mysql_query("SELECT * FROM users WHERE id='$session_id' AND password='$session_pass' LIMIT 1") or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 0){
    while($result = mysql_fetch_array($query)){
        $session_username = $result['username'];
    }
    //create sessions
    $_SESSION['username'] = $session_username;
    $_SESSION['id'] = $session_id;
    $_SESSION['password'] = $session_pass;
    // logged in stuff here
    $logged =1;
} else {
    header("Location: http://www.example.com/logout.php");
    exit();
}
} else {
// if the user is not logged in
$logged = 0;
}
?>

I have also heard I should switch to mysqli and am trying to research on translating my code to include that, but would like to try to focus on getting this working at all first, before securing it. If anyone has time to assist with the main problem, as well as show how I could integrate mysqli, that would be much appreciated as well! :-)

  • 写回答

1条回答 默认 最新

  • doudeng2025 2014-09-03 02:49
    关注

    In your securing section, change

    $password = sha1($pass1);
    

    To

    $password = sha1($password);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)