gpt4 book ai didi

javascript - 使用 php session 时出错

转载 作者:行者123 更新时间:2023-11-30 11:37:30 26 4
gpt4 key购买 nike

我正在开发一个使用 JS、JQuery 和 PHP 的简单登录页面。

登录.php

<!DOCTYPE html>
<head>
<title>Login activity to learn AJAX</title>
<link rel="stylesheet" type="text/css" href="login.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="login.js"></script>
</head>
<body>
<h1>AJAX learning</h1>
<h2>Login below to continue</h2>
<div class="row">
<p>User ID:</p>
</div>
<div class="row">
<p><input type="text" name="uid" id="usr"></p>
</div>
<div class="row">
<p>Password:</p>
</div>
<div class="row">
<p><input type="password" name="pwd"></p>
</div>
<div class="row">
<p><input type="button" onclick="checkLogin()" value="Login"></p>
</div>
<div class="row">
<p id="status"></p>
</div>
</body>

登录.js

function ajaxRequest(){
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}
function checkLogin(){
var xhttp=new ajaxRequest();
var uid=$('input[name=uid]').val();
var pwd=$('input[name=pwd]').val();
xhttp.open("POST","do_login.php",true);
xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var param = "UID="+uid+"&PASS="+pwd;
xhttp.send(param);
xhttp.onreadystatechange=function(){
if(xhttp.readyState == 4){
if(xhttp.status==200){
generateResponse(xhttp.responseText,uid);
}
else{
window.alert("Error while making request");
}
}
}
}
function generateResponse(data,uid){
if(data==="Error 1"){
$("#status").text("USER NOT FOUND!");
$("input[name=uid]").val("");
$("input[name=pwd]").val("");
}
else if(data==="Error 2"){
$("#status").text("INCORRECT PASSWORD!");
$("input[name=pwd]").val("");
}
else{
window.location='page2.php';
}
}

do_login.php

<?php
session_start();
$uid=$_POST['UID'];
$pass=$_POST['PASS'];

$connection = mysqli_connect("localhost","shreyansh","","ajax");
$query1 = "SELECT user FROM login WHERE user = '$uid' AND pass = '$pass' ";
$query2 = "SELECT user FROM login WHERE user = '$uid'";
$result = mysqli_fetch_array(mysqli_query($connection,$query1));
// checking credentials
$response = "";
if(count($result)==0){
$res = mysqli_fetch_array(mysqli_query($connection,$query2));
if(count($res)==0){
$response = "Error 1";
}
else
$response = "Error 2";
}
else{
$_SESSION['username']=$uid;
}
echo $response;
?>

page2.php

<?php
session_start();
if(!isset($_SESSION['username']) || empty($_SESSION['username']))
header("Location: login.php");
?>
<!DOCTYPE html>
<head>
<title>Welcome Page</title>
<script type="text/javascript" src="login.js"></script>
</head>
<body>
<p>Hello <?php echo $_SESSION['username']?> </p>
</body>

主要目的是在 login.php 页面中显示错误消息,如果凭据正确,则重定向到 page2.php
在按下登录按钮时,页面被重定向到 page2.php 但没有显示任何内容。整个网页是空白的。请找出我的代码中的错误。

最佳答案

你的代码有错别字:

<?php
session_start();
if(!isset($_SESSION(['username'])) || empty($_SESSION['username']))
header("Location: login.php");
?>

$_SESSION 不是函数,因此将它与 () 一起使用将返回错误。也只是一个标题不会阻止页面运行,所以你的代码仍然会被执行。设置位置 header 后必须阻止代码执行

<?php
session_start();
if(!isset($_SESSION['username']) || empty($_SESSION['username'])) {
header("Location: login.php");
die();
}
?>

您的 sql "SELECT user FROM login WHERE user = '$uid'" https://en.wikipedia.org/wiki/SQL_injection 中也有 mysql 注入(inject)

尝试使用 ' 或 1 或 'a 作为用户名

防止它的最简单方法是使用 addslashes 包含传递给您的查询的变量:

"SELECT user FROM login WHERE user = '" . addslashes($uid) . "'"

关于javascript - 使用 php session 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43843082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com