我创建了一个php脚本,该脚本将用户名和密码的数据发布到链接到数据库(mySQLI)的表单中,该表单检查传递的数据并登录用户。当我以前直接从表单输入数据时,它可以正常工作。当我通过PHP-curl发送通过登录名时,但是当它重定向到登录页面时,它显示错误,表明会话变量未初始化。有什么办法可以通过会话cookie或使用具有会话ID的cookie存储等等?
首先,我尝试正常传递数据并工作,但是在添加了php-curl代码后,它不起作用,尝试了其他问题的代码,仍然存在相同的问题。我得到了PHPSESSION ID,但是不确定如何利用它将详细信息发送到服务器端,以验证它与看起来像单一登录系统上的用户身份相同的用户
PHP-curl文件 session_start();
$cookie="C:/xampp/htdocs/cookies.txt";
$url="http://localhost/login.php";
$postFields = array('user'=> "2001",
"pass"=> "qw12",
"submit" => "submit" );
echo $_COOKIE["PHPSESSID"];
session_write_close();
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
$response = curl_exec($ch);
//echo $response;
if(!$response){
echo "logged out";
}
else{
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_URL, "http://localhost/loginSuccess.php");
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
echo $response;
}
curl_close($ch);
登录后,应重定向至此页面 这是loginin.php文件,其中会话变量未定义
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Log in</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<style>
.display-4 {
font-size: 2.5rem;
font-weight: 300;
line-height: 1.2;
}
</style>
</head>
<body>
<div>
<nav class="navbar navbar-expand-lg navbar-light" style="background-color: #E74C3C; margin:0px" >
<div ></div>
<span class="navbar-brand text-light" >Uni details</span>
<br> <br>
</div>
<?php
echo $_COOKIE["PHPSESSID"];
if(isset($_SESSION['uid'])){
echo '<div style="width: 62%;" ></div>';
//login echo '<span style=" color:white; margin-right: 10px;">'.$_SESSION['uid'].'</span>';
echo '<form action="databaseConnection/logout.php" method="POST">
<button type="submit" name="submit" class="btn-dark " style="border-radius: 5px" >logout</button>
</form> ';
}
else{
header("Location: login.php");
}
?>
</div>
</nav>
</div>
<!--jumbotron-->
<?php
if($_SESSION['uid'] == 2001)
{
echo '<div class="jumbotron" align="left" style="text-indent: 6.5%; background-color: transparent">
<img src="img/aau-logo.png" style="width:10%; height:10%;" alt="aau-logo">
<h1 class="display-4" style="text-indent:6.5%; font-weight: 500">You are logged in as: </h1>
<div class="container" style="text-indent:0%;">
<ul class="list-group ">
<li class="list-group-item" style="font-weight:500; font-size: 25px;text-align:center"> Admin </li>
</ul>
<br> <br>
<button><a href="sso.php">Moodle test page</a></button>
</div>
</div>';
}
else{
echo '<div class="jumbotron" align="left" style="text-indent: 6.5%; background-color: transparent">
<img src="img/aau-logo.png" style="width:10%; height:10%;" alt="aau-logo">
<h1 class="display-4" style="text-indent:6.5%; font-weight: 500">You are logged in as: </h1>
<div class="container" style="text-indent:0%;">
<ul class="list-group ">
<li class="list-group-item "> Student ID:';?> <?php echo $_SESSION['uid']; ?> <?php echo ' </li>
<br>
<li class="list-group-item "> College:';?> <?php echo $_SESSION['col']; ?> <?php echo '</li>
</ul>
</div>
</div>';
}
?>
如预期的那样,它应该绕过登录密码,并保持我的登录状态,直到注销或关闭浏览器为止。 只是想保持该域中所有网页的登录状态。