好的,这是我的login.php页面的php代码文件:
<?php
if (isset($_GET['action'])) {
switch (strtolower($_GET['action'])) {
case 'login':
if (isset($_POST['username']) && isset($_POST['password'])) {
if (!validateUser($_POST['username'], $_POST['password'])) {
$_SESSION['error'] = "Bad username or password supplied.";
unset($_GET['action']);
}
}else {
$_SESSION['error'] = "Username and Password are required to login.";
unset($_GET['action']);
}
break;
case 'logout':
if (loggedIn()) {
logoutUser();
header ("Location: ".$domainNow);
}else {
unset($_GET['action']);
}
break;
}
}
if (loggedIn()) {
header ("Location: ".$domainNow);
}elseif (!isset($_GET['action'])) {
$sUsername = "";
if (isset($_POST['username'])) {
$sUsername = $_POST['username'];
}
$sError = "";
if (isset($_SESSION['error'])) {
$sError = '<span id="error">' . $_SESSION['error'] . '</span><br />';
}
$sOutput .= '<h2>Login to NextGenStep</h2><br />
' . $sError . '
<form name="login" method="post" action="/user-action/login">
Username: <input type="text" name="username" value="' . $sUsername . '" /><br />
Password: <input type="password" name="password" value="" /><br /><br />
<div style="padding-left:71px;"><input type="submit" name="submit" value="Login!" /></div>
</form>
<div style="padding-left:131px;"><h4>Create a new <a href="/register">account</a>?</h4></div>';
}
echo $sOutput;
?>
我是PHP和MySQL的新手,所以我想要的是在用户登录后从数据库中获取用户ID(列名为uid),但我也希望它存储用户ID在一个会话中,我可以在其他页面上使用它。我已经有session_start();在包含文件上声明,所以我不必在每个页面上手动执行。
此外,这是我的include页面上的loggedIn函数:
function loggedIn() {
if (isset($_SESSION['loggedin']) && isset($_SESSION['username'])) {
return true;
}
答案 0 :(得分:0)
只需将UID保存在会话变量中,例如;
$_SESSION['uid'] = $uid;
在尝试使用session_start();
变量之前,请记住使用$_SESSION
,否则它将是未定义的。
顺便说一句,你的loggedIn()
函数应该有一个错误的返回以及一个真正的返回。我会用这个;
function loggedIn() {
return ((isset($_SESSION['loggedin']) and isset($_SESSION['username'])) ? true : false);
}
希望这有帮助。
答案 1 :(得分:0)
要从您的数据库中检索您的UID,只要它已写入并且不是空的尝试(无论哪种方式,空或非空):
class FetchUID
{
public static function execute($_uid='')
{
if(!empty($_uid)) {
// I am pressuming you have a global database engine
// If not, retrieve your engine however at this point.
// For sake of ease, I am pretending it's a global....
global $con;
// I use PDO so this is how I would get it
// I am also making the statement broad just to make sure it is retrieving at all
$query = $con->prepare("select * from users where username ='$_uid'");
$query->execute();
if($query->rowCount() == 1)
$user_info = $query->fetch(PDO::FETCH_ASSOC);
else
$user_info['uid'] = self::Write();
}
return (isset($user_info['uid']) && !empty($user_info['uid']))? $user_info['uid']:'error';
}
protected static function Write()
{
// Code here that creates the custom uid and returns it again
}
}
// Check that the session->username is set + that the session->uid is NOT set
if(isset($_SESSION['username']) && !isset($_SESSION['uid'])) {
// If set, fetch it via above class & methods
$uid = FetchUID::execute($_SESSION['username']);
// Write your $uid
echo $uid;
// Sessionize it
$_SESSION['uid'] = $uid;
}