我使用html,css,jquery和php制作页面,这里是登录功能,当我成功登录时,页面会显示我的用户名。但是当我刷新页面时,用户名变成"登录"。每次刷新时它都会自动注销吗?如何防止这种情况发生? 我使用ajax将数据附加到html,如果设置会话我该怎么办?
<?php
session_start();
// include "maroon5/includes/dbh.php";
define ("root",$_SERVER['DOCUMENT_ROOT']);//
include root."maroon5/includes/dbh.php";
if(isset($_SESSION['id'])){
// echo $_SESSION['id'];
//WHAT SHOULD I DO HERE???
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Maroon5 | Official Site</title>
<link rel="stylesheet" type="text/css" href="styles/style.css">
&#13;
$(document).ready(function(){
$(document).on('click',"#submit",function(){
var email = $("#edit-name").val();
var pass = $("#edit-pass").val();
// alert (email);
$.ajax({
url:"functions/php/login.php",
type:"POST",
data:{ email:email,pass:pass},
beforeSend: function () {
},
success:function(data){
console.log(data);
// alert(data);
$("#edit-name").val(" ");
$("#edit-pass").val(" ");
// $("#user-login-register").hide();
$("#popup-box1").hide();
//
$(".user-link-login").css({"display":"none"});
$(".user-menu").append(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
});
return false;
});
});
PHP:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors',1);
define ("root",$_SERVER['DOCUMENT_ROOT']);
// echo root;
include root.'/maroon5/includes/dbh.php';
// var_dump($_REQUEST);
$email = $_POST['email'];
$pass = $_POST['pass'];
$conn = mysqli_connect("localhost", "root", "", "maroon5");
$sql = "SELECT * FROM user WHERE email= '$email' AND pass='$pass' ";
$res = mysqli_query($conn, $sql);
$num = mysqli_num_rows($res);
$row = $res ->fetch_assoc();
if(!$email & !$pass){
echo "All fields required";
}elseif(!$email){
echo "Email required";
}elseif (!$pass) {
echo "Password required";
}elseif ($num == 0) {
echo "wrong";
}else{
// echo "success";
$_SESSION['id'] = $row['id'];
?>
<li class="user-profile">
<a class="" >
<img src = "<?php echo $row["pic"] ?>" />
<span class="user-username"><?php echo $row["username"]; ?></span>
</a>
<ul class="menu-dropdown">
<li class="user-pro"><a href="">VIEW PROFILE</a></li>
<li class="user-verify-account"> <a class="user-parent-account" href=""><i class="fa fa-envelope-o" aria-hidden="true"></i>VERIFY ACCOUNT</a></li>
<li class="user-logout"><a class="user-parent-logout" href=""><i class="fa fa-sign-out" aria-hidden="true"></i>LOGOUT</a></li>
</ul>
</li>
<?php
}
?>
答案 0 :(得分:1)
也许尝试设置保存您的姓名或唯一ID的会话变量。这样,您可以在更轻松地刷新时重新加载信息。