我的页面登录状态有问题。我实际上让它工作但我使用两个htmls这样做。我想知道我是否可以只用一个html来防止页面的冗余仅用于标题。下面是我使用的代码,php和html的混合。虽然当我运行它似乎工作,但它运行时没有获得$ userRow值,只是一个“嗨!”。另外,我似乎无法执行第二个PHP的else函数。每当用户注销时,它只会将我重定向到登录表单。
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: login.php");
}
else {
$_SESSION['user'] = true;
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
?>
<body>
<div id = "header">
<ul>
<?php
if ($_SESSION['user'] == true) {
//I want the below codes to only be seen when the user has successfully logged in.
echo '<div style = "float: right"><li><a href = "profile.php">' . "Hi!" . $userRow['user_name'] . '</a>' . '<a href="logout.php?logout">' . "Sign Out" . '</a></li></div>';
} else {
//I want this code to be seen when the user has not logged in yet. Making this like the default set up of the home page when the user hasn't logged in yet.
echo '<div style = "float: right"><li><a href="http://localhost/reg2/login.php">' . "Login" . '</a></li></div>' ;
echo '<div style = "float: right"><li><a href="http://localhost/reg2/signup.php">' . "Sign Up" . '</a></li></div>' ;
}
?>
</ul>
</div>
以下是我登录http://i.imgur.com/Tfvemt4.png
时的结果图以下是我还没有登录时的想法:http://i.imgur.com/Wm1TrcD.png 正如我所说,第二个输出没有显示,每当我签出它只会重定向到登录页面,这是完全另一个PHP文件。
编辑:(修好了问题,下面的工作代码。感谢您提供的答案。:D)
<body>
<div id = "header">
<ul>
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
echo '<div style = "float: right"><li><a href="http://localhost/reg2/login.php">' . "Login" . '</a></li></div>' ;
echo '<div style = "float: right"><li><a href="http://localhost/reg2/signup.php">' . "Sign Up" . '</a></li></div>' ;
}
else {
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
echo '<div style = "float: right"><li><a href = "profile.php">' . "Hi!" . $userRow['user_name'] . '</a>' . '<a href="logout.php?logout">' . "Sign Out" . '</a></li></div>';
}
?>
</ul>
</div>
答案 0 :(得分:0)
试试这个
$('document').on('click', '.more_notes_link', function() {
// your code
}