我制作了一个基本的 HTML/PHP 登录页面和 jQuery 移动登录页面。我想让它们都提交到同一个名为 checklogin.php 的文件,如果登录成功,则旨在将它们重定向到各自的主页。它适用于基本页面,但不适用于 jQuery Mobile 页面。尝试通过移动页面提交会将我带到一个带有“未定义”短语的页面。
因此,我尝试编写一些 PHP 和 jQuery 脚本,以便如果检测到移动帖子,而不是立即转到主页,它会返回成功,并且登录页面本身会将用户重定向到主页面页。尽管出于测试目的单击提交会产生一个成功的“表单已发送”警报,但正确的登录不会使页面发生变化。我应该添加什么?
这是我的 PHP 登录检查器。
<?php
$host=// Host name
$username=// Mysql username
$password=// Mysql password
$db_name=// Database name
$tbl_name=// Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To prevent MySQL injection (a form of internet hacking)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
// Register $myusername, $mypassword and redirect to the main page"
session_register("myusername");
session_register("mypassword");
//Send them back to the page they were at/
if(!$_POST['mobile'])
{
header("location:main.php");
}
else
{
header('HTTP/1.0 401 Unauthorized', true, 401);
}
}
else
{
echo "Wrong Username or Password";
}
?>
这是我在登录页面上用于重定向用户的脚本。
<script language="javascript">
// Global declarations - assignments made in $(document).ready() below
var hdrMainvar = null;
var contentMainVar = null;
var ftrMainVar = null;
var contentTransitionVar = null;
var stateLabelVar = null;
var whatLabelVar = null;
var stateVar = null;
var whatVar = null;
var form1var = null;
var confirmationVar = null;
var contentDialogVar = null;
var hdrConfirmationVar = null;
var contentConfirmationVar = null;
var ftrConfirmationVar = null;
var inputMapVar = null;
// Constants
var MISSING = "missing";
var EMPTY = "";
var NO_STATE = "ZZ";
function getFormValues() {
data = {};
data['username'] = $("#username").val();
data['password'] = $("#password").val();
return data;
}
function transmitPost() {
$.ajax({
url: 'checklogin.php',
type:"post",
data: getFormValues(),
statusCode: {
401: function() {
alert("Wrong Password");
},
200: function() {
window.location = "m/main.php";
alert('Submitted');}
}
error: function(value) {
alert("error");
}
});
}
$('#submitButton').bind('click', function() {
alert('User clicked on "foo."');
});
</script>