只是想知道是否有人可以帮助我解决我的最新问题。我是编程新手,非常感谢我在这里获得的帮助(所以请耐心等待,我们都必须从某个地方开始!):)
基本上我有这个应用程序,我已经为它创建了一个登录名,它可以很好地验证信息,并在你成功登录后将其带到下一页。
我无法理解的是用它启动一个 PHP 会话?最后我想要实现的是:
- 为登录用户启动 PHP 会话
- 在迷你应用程序中携带用户 ID,以便以后可以将其以另一种形式插入到数据库中,而无需用户自己输入
我的html代码是:
<!DOCTYPE html>
<html>
<head>
<title>Find A Deal</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="http://localhost/findadeal/themes/deal.css" />
<style>
#login-button {
margin-top: 30px;
}
</style>
<script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="js/custom3.js"></script>
</head>
<body>
<div data-role="page" id="login">
<div data-theme="a" data-role="header">
<h3>Find A Deal</h3>
</div>
<div data-role="content">
<?php
if( !isset( $_SESSION ) ){
session_start();
}
if( isset( $_SESSION['username'] ) ){
/* User is logged in */
}
?>
<label for="username">Enter your username:</label>
<input type="text" value="" name="username" id="username"/>
<label for="password">Enter your password:</label>
<input type="password" value="" name="password" id="password"/>
<a data-role="button" id="login-button" data-theme="b">Login</a>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
<!--Newly rendered page after successful login!-->
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h2>Find A Deal</h2>
</div>
<div data-role="content">
<h3></h3>
<a href="#view" data-role="button" data-icon="search">View Deals</a>
<a href="http://localhost/findadeal/login/newdeal.php" data-role="button" data-icon="plus">Add Deals</a>
</div>
</body>
</html>
这是创建 Ajax 请求等的 Javascript 函数:
$(document).on('pagebeforeshow', '#login', function(){
$('#login-button').on('click', function(){
if($('#username').val().length > 0 && $('#password').val().length > 0){
userObject.username = $('#username').val(); // Put username into the object
userObject.password = $('#password').val(); // Put password into the object
// Convert an userObject to a JSON string representation
var outputJSON = JSON.stringify(userObject);
// Send data to server through ajax call
// action is functionality we want to call and outputJSON is our data
ajax.sendRequest({action : 'login', outputJSON : outputJSON});
} else {
alert('Please fill all nececery fields');
}
});
});
$(document).on('pagebeforeshow', '#index', function(){
if(userObject.username.length == 0){ // If username is not set (lets say after force page refresh) get us back to the login page
$.mobile.changePage( "#login", { transition: "slide"} ); // In case result is true change page to Index
}
$(this).find('[data-role="content"] h3').append('Welcome ' + userObject.username); // Change header with wellcome msg
//$("#index").trigger('pagecreate');
});
// This will be an ajax function set
var ajax = {
sendRequest:function(save_data){
$.ajax({url: 'http://localhost/findadeal/login/json2.php',
data: save_data,
async: true,
beforeSend: function() {
// This callback function will trigger before data is sent
$.mobile.showPageLoadingMsg(true); // This will show ajax spinner
},
complete: function() {
// This callback function will trigger on data sent/received complete
$.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
},
success: function (result) {
if(result == "true") {
$.mobile.changePage( "#index", { transition: "slide"} ); // In case result is true change page to Index
} else {
alert('Login unsuccessful, please try again!'); // In case result is false throw an error
}
// This callback function will trigger on successful action
},
error: function (request,error) {
// This callback function will trigger on unsuccessful action
alert('Network error has occurred please try again!');
}
});
}
}
// object to store username and password.
var userObject = {
username : "",
password : ""
}
最后这是我的 PHP 文件:
<?php
session_start();
$var1 = $_REQUEST['action'];
$jsonObject = json_decode($_REQUEST['outputJSON']); // Decode JSON object into readable PHP object
$username = $jsonObject->{'username'}; // Get username from object
$password = $jsonObject->{'password'}; // Get password from object
mysql_connect("localhost","root",""); // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password
@mysql_select_db("findadeal") or die( "Unable to select database"); // Connect to database called test
$query = "SELECT * FROM restaurant WHERE username = '".$username."' and password = '".$password."'";
$result=mysql_query($query);
$num = mysql_numrows($result);
if ($num != 0) {
$_SESSION['username'] = $username;
}
else {
echo "false";
}
?>
如果有人可以帮助我,那就太棒了!我想我已经在 HTML 端开始了会话,并且 javascript 具有正确的元素,它在 PHP 端弄清楚它是我倾向于迷失自己的地方。我试图让用户 id 在应用程序的各种形式中传递,但是有没有一种方法可以在不需要他们插入的情况下完成?