【发布时间】:2013-04-10 15:03:24
【问题描述】:
我正在尝试创建登录页面。
现在您只需在 login.php 页面上单击 Login,然后它会向 index.php 发送一个请求,该请求会显示成功登录的消息并更改变量loggedin在会话中用于查看用户是否登录,然后重定向到主页。
现在我的 导航栏 (nav.php) 检查会话变量loggedin 并相应地显示内容,即用户是否登录以及他们是否可以看到我的个人资料链接等...
问题是当登录页面 (login.php) 向 index.php 发送登录页面的请求时,它会进行如下检查:
index.php:
.row, .col { overflow: hidden; position: absolute; }
.row { left: 0; right: 0; }
...
.menu.row { height: 75px; top: 75px; background-color: #EDEDED; }
...
<div class="menu row">
<?php
include("nav.php"); ?>
</div>
...
case 'logged_in':
$_SESSION['loggedin'] = true;
print '<script type="text/javascript">';
print 'alert("You have been logged in successfully and will be re-directed to your homepage...")';
print '</script>';
include('home.html');
echo '<script type="text/javascript">$(".menu.row").load("index.php");</script>';
break;
如您所见,最后一个 echo 语句尝试更新包含 nav.php 的菜单行 div 类,以反映正在显示的会话变量 loggedin 的更改,但它仅在我单击另一个链接后才显示更改nav.php 页面。
所以基本上我在问为什么我的
echo '<script type="text/javascript">$(".menu.row").load("index.php");</script>';
不刷新包含我的 nav.php 的 div,因此允许 nav.php 执行 PHP 代码以检查变量 loggedin 并采取相应措施?
以下是部分代码:
index.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CC Chat</title>
<meta
http-equiv="content-type" content="text/html; charset=utf-8"
/>
<!--
Thanks too http://stackoverflow.com/a/7851347/1133011 for the help
on layout which acts more like frames but avoids them and using divs. As frames have
known draw backs see here http://stackoverflow.com/questions/4600648/frames-with-php we
should thus rather use include in php
!-->
<style type="text/css" media="screen">
/* Generic pane rules */
body { margin: 0 }
.row, .col { overflow: hidden; position: absolute; }
.row { left: 0; right: 0; }
.col { top: 0; bottom: 0; }
.scroll-x { overflow-x: auto; }
.scroll-y { overflow-y: auto; }
.header.row { height: 75px; top: 0; background-color: #E5E5E5; }
.menu.row { height: 75px; top: 75px; background-color: #EDEDED; }
.body.row { top: 150px; bottom: 50px; background-color: #F5F5F5; }
.footer.row { height: 75px; bottom: 0; background-color: #EBEBEB; }
</style>
</head>
<body>
<div class="header row">
<?php include("header.html"); ?>
</div>
<div class="menu row">
<?php
include("nav.php"); ?>
</div>
<div class="body row scroll-y">
<?php
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'logged_in':
$_SESSION['loggedin'] = true;
print '<script type="text/javascript">';
print 'alert("You have been logged in successfully and will be re-directed to your homepage...")';
print '</script>';
include('home.html');
echo '<script type="text/javascript">$(".menu.row").load("index.php");</script>';
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else
include('home.html');
?>
</div>
<div class="footer row">
<?php include("footer.php"); ?>
</div>
</body>
</html>
nav.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Navigator</title>
<meta
http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<p align="center">
<?php
if(isset($_SESSION['loggedin'])) {
switch ($_SESSION['loggedin']) {
case true:
echo "<a href=\"index.php?page=home\">Home</a> <a href=\"index.php?page=contact_us\">Contact Us</a> <a href=\"index.php?page=profile\">My profile</a> <a href=\"index.php?page=log_out\">Log out</a>";
break;
default:
echo "<a href=\"index.php?page=home\">Home</a> <a href=\"index.php?page=contact_us\">Contact Us</a> <a href=\"index.php?page=login\">Login</a>";
break;
}
} else {
echo "<a href=\"index.php?page=home\">Home</a> <a href=\"index.php?page=contact_us\">Contact Us</a> <a href=\"index.php?page=login\">Login</a>";
}
?>
</p>
</body>
</html>
登录.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
</head>
<body>
<h2 align="center">Login</h2>
<p align="center">
<?php
$var=true;//user password was correct
if($var==true){
echo '<a href="index.php?page=logged_in">Login</a>';
} else {
print '<script type="text/javascript">';
print 'alert("Password is incorrect.")';
print '</script>';
}
?>
</p>
</body>
</html>
【问题讨论】:
-
你在
echo '<script type="text/javascript">$(".menu.row").load("index.php");</script>';中使用 jQuery,但是 jQuery 是在哪里加载的? -
@j08691 是 JQuery 吗?击败我,我从另一个答案中得到它,所以现在找不到它来链接它,但这就是 OP 在
include(..)之后标记为在 php 中引用 div 的答案...... -
是的,那是 jQuery,如果你不包括 jQuery,那么它就不会做任何事情。
-
好的,但是我看到我有
text/javascript"我尝试更改为echo '<script type="text/jquery">$(".menu.row").load("index.php");</script>';但在我按下登录后仍然没有效果我仍然需要单击nav.php 上的链接才能刷新跨度> -
jQuery 是一个 javascript 库,它不是自己的语言。