我正在尝试解决我遇到的这个问题。问题是,当有人想登录到管理面板时,我会使用此代码:
<script>
function myFunction() {
//alert('you can type now, end with enter');
$("#test").focus();
}
$(document).ready(function() {
$("form").submit(function(e) {
e.preventDefault();
// alert($("#test").val());
var email = $("#test").val();
if(email==''){
// alert("Error.");
sweetAlert("Oops...", "Error!", "error");
} else {
$.post("sess.php",{ code1: code},
function(data) {
// alert(data);
// swal(data);
if((data)=="1") {
swal("Welcome!", "Please wait!", "success")
} else {
sweetAlert("Oops...", "Something went wrong.", "error");
}
$('#form')[0].reset(); //To reset form fields
});
}
});
});
</script>
Sess.php如下所示:
<?php
include("conn.php");
?>
<?php
include("ipcheck.php");
$code2=htmlEntities($_POST['code1'], ENT_QUOTES);
$info = explode("-", $code2);
$username = $info[0];
$password = $info[1];
$_POST = db_escape($_POST);
$sql = "SELECT id FROM adminusers
WHERE user='{$username}'
AND pass='$password'";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0){
echo "2";
exit;
}
// Session for user
$_SESSION['sess_id'] = mysql_result($result, 0, 'id');
$_SESSION['sess_user'] = $username;
// DAtabse going on here.
echo "1";
exit;
?>
因此,如果用户名和密码正确,则登录成功,并且这些会话设置在sess.hp:中
$_SESSION['sess_id'] = mysql_result($result, 0, 'id');
$_SESSION['sess_user'] = $username;
我的问题是,我如何使用javascript将通过sess.php为用户设置的会话返回到index.php,这样我就可以在index.php中而不是在sess.php中设置会话?
为什么要将会话传递给javascript?我的意思是,如果你在sess.php上设置会话服务器端,你甚至已经在index.php中设置了它(会话存在于整个…session:-)),所以当sess.php返回身份验证正确时,你的javascript应该只将页面移动到index.php,比如:
window.location.href = 'index.php';
在index.php中,如果打印会话print_r($_session),您应该会看到以前在sess.php 中设置的值
如果你的$_SESSION在index.php中是空的,那么你可能必须在使用SESSION_start()读取$_SESSIN之前启动会话;