只是想知道如果有人能幫我解決我最近的問題。我在編程方面非常新,非常感謝我在這裏得到的幫助(所以請耐心等待,我們都必須從某個地方開始!):)jquery PHP會話
基本上我有這個應用程序,我創建了一個登錄它會驗證信息,並在您成功完成登錄後將其帶入下一頁。
我在理解時遇到困難就是啓動一個PHP會話嗎?我想實現在本月底是:
- 爲登錄用戶
- 開展跨迷你應用程序的用戶ID,以便以後能以另一種形式插入啓動一個PHP會話進入用戶不用給它自己鍵入
我的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>
這是JavaScript函數創建Ajax請求ETC:
$(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跨應用程序的各種形式傳遞,但有沒有一種方法可以做到這一點,而不需要他們插入它?
因此,所有我需要做的只是設置一個名爲用戶名和IM登錄會話?不是很安全。還讀了一些關於不推薦使用的'mysql_ *'函數和'mysql注入'的內容。你寫了一個黑客玩具,如果你不知道... – 2013-02-15 15:02:12