douxing6532 2016-08-19 03:47
浏览 47
已采纳

登录页面允许任何密码登录php

i have a problem with my code when i add this

Login.php

 <?PHP session_start();
 $_SESSION['email'] = $_POST['email']; 

?>
<?php echo '<script>window.location = "http://somepage.com/"</script>'; ?>

my login page allows any password to log in, it displays the correct email address in my home page. but when i delete the code above and type any password it will not let me log in i have to use the correct password. why would this happen? i really need the code above to work because that allows me to log in to restricted pages this is what i use in restricted pages like profile.php

resctricted pages like profile.php

<?PHP
session_start();
if(!$_SESSION['email']){
header("Location: login");
die;
}
?>

<?PHP session_start();
$_SESSION['email'] = $_POST['email']; ?>

<?php echo '<script>window.location = "http://torcdesign.com/"</script>'; ?>

<?php 
    require_once("configur.php");
    $mysqli = new mysqli(localhost, root, password, dbname);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }
 
$email=$_POST['email']; 


$encrypt_password = $_POST['encrypt_password']; 
$encrypt_password = hash("sha512",$encrypt_password);
 
    $sql = "SELECT * from register_login WHERE  email='$email' and encrypt_password='$encrypt_password'";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
        echo "<p>Invalid username/password combination</p>";
    } else {
        echo "<p>Logged in successfully</p>";
        // do stuffs
    }

?>       

</div>
  • 写回答

1条回答 默认 最新

  • donglaohua1671 2016-08-19 03:52
    关注

    Dont set the session before you check the condition

    <?PHP session_start(); ?>
    
    <?php echo '<script>window.location = "http://torcdesign.com/"</script>'; ?>
    
    <?php 
        require_once("configur.php");
        $mysqli = new mysqli(localhost, root, password, dbname);
        # check connection
        if ($mysqli->connect_errno) {
            echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
            exit();
        }
    
    $email=$_POST['email']; 
    
    
    $encrypt_password = $_POST['encrypt_password']; 
    $encrypt_password = hash("sha512",$encrypt_password);
    
        $sql = "SELECT * from register_login WHERE  email='$email' and encrypt_password='$encrypt_password'";
        $result = $mysqli->query($sql);
        if (!$result->num_rows == 1) {
            echo "<p>Invalid username/password combination</p>";
        } else {
            $_SESSION['email'] = $email;
            echo "<p>Logged in successfully</p>";
            // do stuffs
        }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题