想知道是否有人可以帮助我处理我的最新一期。我非常擅长编程,非常感谢我在这里得到的帮助(所以忍受我,我们都必须从某个地方开始!):)
基本上我有这个应用程序,我已经为它创建了一个登录,它可以很好地验证信息,并在您成功登录后将其带到下一页。
我遇到的问题是用它启动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>
这是创建Ajax请求的Javascript函数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通过各种形式的应用程序传递,但有没有办法可以在没有它们插入的情况下完成?
答案 0 :(得分:3)
你有两件事你希望完成
通过将数据存储在$_SESSION数组中,您可以访问用户访问的每个PHP脚本中的数据。
为了能够访问会话数组,您必须在访问session_start();
数组之前调用$_SESSION
。请记住,每次执行PHP时只能调用session_start一次。如果多次调用session_start,则会生成警告或错误(我不记得是哪个)
您可以检查$_SESSION
数组是否设置为知道会话是否已启动
if( !isset( $_SESSION ) ){
session_start();
}
if( isset( $_SESSION['username'] ) ){
/* User is logged in */
}
您可能还想查看此相关问题PHP User Authentication with Sessions
<强> ANSWER 强>: @ user2025934
1)session_start();必须只调用一次,然后开始 数组,我在你的结尾添加了上面的mini php if语句 示例到我想要携带的所有php / html文件的顶部 是的值?考虑到这一点,我用我的代码实现了我所需要的 以上? (我已根据您的更改对其进行了编辑)
是的,添加此代码段可让您在放置它的任何页面上访问$_SESSION
数组。
if( !isset( $_SESSION ) ){
session_start();
}
2)另外,有没有办法可以测试它以确保其正常工作?为了确保它正常工作我&gt;假设我将echo函数添加到用户用户名的第二个if语句中 会输出吗? -
Echo`ng一些输出如下所示是一种或多种方式来测试它是否正常工作
if( isset( $_SESSION['username'] ) ){
/* User is logged in */
echo "It Works! - The user is logged in!";
}
答案 1 :(得分:1)
将session_start();
放在PHP文件的顶部。然后,当他们成功登录时,您可以这样做:
if ($num != 0) {
$_SESSION['username'] = $username;
}
这会将用户名保存到会话中,这样您就可以在会话期间跨PHP页面访问用户的用户名。