如果用户有会话,我正在运行以下 javascript 来检查服务器端 (keep.php)。该脚本正在运行每个页面的 onload:handler.js
var server = location.host;
var url = "http://" + server + "/MyHome2/php/";
var page = "keep.php";
document.onload = checkAuth();
function checkAuth()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
if (response != "")
{
document.getElementById("linkSign").innerHTML = response + "|" ;
}
else
document.getElementById("linkSign").innerHTML = "Sign-in|" ;
}
}
xmlhttp.open("POST",url + page,true);
xmlhttp.send();}
我的服务器端 keep.php 回显会话是:
<?php
session_start();
if (isset($_SESSION['name']))
{
echo $_SESSION['name'];
}
?>
该脚本在每个 html 页面的请求中运行,以检查用户是否有会话,如果有会话,他会感到厌烦。该脚本与我的其他脚本混淆的问题!在没有这个 handler.js 的情况下可以正常工作并且可以正常工作
例如,当用户尝试提交他的凭据时,我的登录脚本无法登录用户,注意发生了!
var server = location.host;
var url = "http://" + server + "/MyHome2/php/";
var urlmain = "http://" + server + "/MyHome2/index.html";
var page = "authentication.php";
function loginUser(){
var str = "usr=" + encodeURIComponent(document.getElementById("user").value) + "&pwd=" + encodeURIComponent(document.getElementById("password").value);
checkUserExistance(str,str.length);}
function checkUserExistance(data,size){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
responseHandler(response);
}
}
xmlhttp.open("POST",url + page,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length",size);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(data);}
为什么每个页面的简单会话检查 onload 会阻止 mt 其他脚本执行?!