doxd96148 2015-09-24 06:08
浏览 36

记住我使用cookie的功能不允许通过其他登录详细信息登录php

I created a login page that contain email id/username, password and remember me checkbox. For the first time if i checked the remember me checkbox it works fine. If i try to login through other email address or password and untick remember me checkbox then it login with the previous email id and password. For example : I logged in with xyz@gmail.com email id and select remember me checkbox then login work fine. After that i again logged in with another email id suppose abc@gmail.com an untick remember me then it log in me through the previous email id that is xyz@gmail.com.

I don't understand why this is happening.

Here is my code:

HTML:

<div class="row form-row">
            <div class="col-md-12"><div class="input-with-icon right">                                       
               <input type="text" placeholder="<?php echo _('Email Id/Username'); ?>" class="form-control"  title="<?php echo _('Enter your Email Id/Username'); ?>" name="email_id" value="<?php
                if (isset($_COOKIE['email_id'])) {
                echo $_COOKIE['email_id'];
                }
                ?>"/>
            </div>
            </div>
        </div>

        <div class="row form-row">
            <div class="col-md-12">
            <div class="input-with-icon right">                                       

                <input name="password"  type="password" placeholder="<?php echo _('Password'); ?>" class="form-control"  id="password" title="<?php echo _('Enter your password'); ?>"  value="<?php
                if (isset($_COOKIE['password'])) {
                echo $_COOKIE['password'];
                }
                ?>"/>
            </div>
            </div>
        </div>
        <br/>
        <div class="row">
            <div class="col-md-6 Heading-text"> 
                <div class="checkbox2">
                    <label><input type="checkbox" name="remember" <?php echo isset($_COOKIE['remember_me']) ? "checked='checked'" : "";?> id="remember"><span style="color:black;"><?php echo _('Remember Me'); ?></span></label>
                </div>
            </div>
        </div>          
        <br/>
        <div class="input-group center-block text-right">
            <button class="btn btn-info btn-cons center-block center-text " type="submit" name="user_login"><?php echo _('Login'); ?></button>                  
        </div>

This is my PHP Code:

if(isset($_REQUEST['user_login'])
{
   $data = $_REQUEST;
   $email_id = $data['email_id'];   

$password = $data['password'];


$sql = "SELECT userid,username FROM users WHERE password = '" . $password."' AND (email_id = '" . $email_id . "' OR username = '".mysqli_real_escape_string($this->getConnection(),$email_id)."')";
if ($this->checkRecordExists($sql)) {
    $result = $this->getRecords($sql);

            if (isset($data['remember']) && $data['remember'] == 'on') 
            {
                /*
                 * Set Cookie from here for one hour
                 */
                setcookie("email_id", $data['email_id'], time() + (3600 * 24 * 30));
                setcookie("password", $data['password'], time() + (3600 * 24 * 30));  /* expire in 1 hour */
                setcookie("remember_me", 1, time() + (3600 * 24 * 30));
            } else 
            {
                /**
                 * Following code will unset the cookie
                 * it set cookie 1 sec back to current Unix time
                 * so that it will invalid
                 */
                setcookie("email_id", $data['email_id'], time()-1);
                setcookie("password", $data['password'], time()-1);
                setcookie("remember_me", 1, time()-1);
            }

            $_SESSION['user_info']['username'] = stripcslashes(urldecode($result[0]['username']));
            $_SESSION['user_info']['userid'] = $result[0]['userid'];


    }
}

Kindly help me where i am wrong.

  • 写回答

1条回答 默认 最新

  • douzhang6176 2015-09-24 08:16
    关注

    I found the fix. The problem is because i was setting cookie variable name same as input control name and when i submit my form cookie value overrides the input control value.

    Here is the fixed code:

    if (isset($data['remember']) && $data['remember'] == 'on') 
            {
                /*
                 * Set Cookie from here for one hour
                 */
                setcookie("user_email_id", $data['email_id'], time() + (3600 * 24 * 30));
                setcookie("user_password", $data['password'], time() + (3600 * 24 * 30));  /* expire in 1 hour */
                setcookie("user_remember_me", 1, time() + (3600 * 24 * 30));
            } else 
            {
                /**
                 * Following code will unset the cookie
                 * it set cookie 1 sec back to current Unix time
                 * so that it will invalid
                 */
                setcookie("user_email_id", $data['email_id'], time()-1);
                setcookie("user_password", $data['password'], time()-1);
                setcookie("user_remember_me", 1, time()-1);
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题