我正在使用HTML5 App开发Phonegap,
数据库驻留在我的服务器上。注册和登录是通过ajax帖子完成的,我对每个用户都有三个不同级别的访问权限。
应用程序流程:从用户那里获取输入,并在服务器端“数据库”对其进行验证,然后显示结果,可能是成功的,也可能是失败的,或者是用户的类型。
问题:当我得到响应后,如何重定向到另一个页面,并将用户名保存在页面上,以及如何注销用户。
这是我的密码
index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login/register</title>
<script language="JavaScript" type="text/javascript">
function ajax_post(){
var databox =document.getElementById("status");
var hr = new XMLHttpRequest();
var url = "login.php";
var u = document.getElementById("username").value;
var p = document.getElementById("password").value;
var f = document.getElementById("format").value;
var vars = "username="+u+"&password="+p+"&format="+f;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = JSON.parse(hr.responseText);
if(return_data.Status == "success"){
if(return_data.type == "Collecting Data"){
//first type of user
window.location.href = "a.html";
}
else{
if(return_data.type == "Supervisor"){
//second type of user
}
else{
//third type of user
}
}
}
else{
if(return_data.Status == "fail"){
databox.innerHTML=" please try again ";
}
}
}//end if
}//end fuction
hr.send(vars); // Actually execute the request
}//end ajax_post function
</script>
</head>
<body>
<table width="400px">
<tr>
<td colspan="2" style="text-align:center">
<h3>Log in/Register</h3>
</td>
</tr>
<tr>
<td valign="top">
<label for="username">Username *</label>
</td>
<td valign="top">
<input id="username" name="username" type="text" maxlength="50" size="25"/>
</td>
</tr>
<tr>
<td valign="top">
<label for="password">Password *</label>
</td>
<td valign="top">
<input id="password" name="password" type="password" maxlength="50" size="25"/>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input id="format" name="format" type="hidden" value="json"/>
<input name="myBtn" type="submit" value="Login" onClick="javascript:ajax_post();">
</td>
</tr>
</table>
<br /><br />
<div id="status"></div>
</body>
</html>
这是login.php
<?php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);
$format= $_POST['format'];
mysql_connect("localhost","user","pass") or die (mysql_error());
mysql_select_db("database name") or die ("no database");
$sqlString = mysql_query("SELECT * FROM staff WHERE username = '$username' AND password = '$password'");
$row = mysql_fetch_array($sqlString);
$num_results = mysql_num_rows($sqlString);
if($num_results ==1){
$Status='success';
$type=$row["type"];
$_SESSION['username'] = $username;
deliver_response($username,$password,$Status,$type,$format);
}
else{
$Status='fail';
$type="empty";
deliver_response($username,$password,$Status,$type,$format);
}
function deliver_response($u,$p,$s,$t,$f){
if($f == 'json') {
$response['username']=$u;
$response['password']=$p;
$response['type']=$t;
$response['Status']=$s;
header('Content-type: application/json');
$json_response=json_encode($response);
echo $json_response;
}//end if
else{
echo 'format not valid';
}//end else
}//end function
?>
当我返回响应并重定向到另一个html页面并转到php文件时,抛出ajax并在php中使用此语句。
echo $_SESSION['username'];
它将打印空。
发布于 2014-03-28 06:39:48
使用此方法获取响应:How do I return the response from an asynchronous call?
为了获得名称,只需将其保存为$_SESSION变量或cookie。然后,通过这样的操作(在标题中打印它)就可以在页面上找到它。
<!DOCTYPE html>
<html>
<head>
<?php
$name = $_SESSION['name'];
echo "<title>".$name."</title>";
?>
</head>
另外,发布一些代码来向我们展示您已经尝试过的内容。
发布于 2014-03-28 07:09:58
基本上,您需要在后端系统上进行会话,对于登录的Ajax请求将检查后端系统是否成功地进行身份验证。我建议您为基于Ajax的登录实现以下序列。
登录过程
注销过程
$.getJSON('user/login',{user:'user',密码:‘password’},函数(响应){ if(response.result){ location.href =‘/home’});
请注意,提出的流程是非常基本的,可以做更多的改进,以便更好地控制。
https://stackoverflow.com/questions/22715728
复制