请问,之前发过类似的帖子,可能由于问题没有说清楚,所以没有得到合适的答案。所以就再发一个,多多包涵。对于后台管理系统,假设 ,当在浏览器输入 localhost/test/admin.php 后,输入用户名 和密码,点击 登录 按钮。这时候会显示 登录页面index.php内容,而这时候 浏览器的地址栏就会变成 localhost/test/admin.php ?act=index请问 浏览器地址栏变成localhost/test/admin.php ?act=index 中 的 ?act=index,这个怎么实现的呢?麻烦知道的朋友帮忙回复,谢谢啦
@u011516112 谢谢您的回复请问 你指的路由是指使用 thinkphp框架所实现的路由功能吗?我对thinkphp不太熟悉。所以就想不用任何框架的。麻烦再帮忙详细回复下,谢谢啦
在 admin.php 中验证登录成功后
header("Location: admin.php?act=index"); /* 重定向浏览器 */
@ xuzuning 谢谢您的回复
为什么我以下代码是实现不了跳转的呢? admin .php 和 index.php在同一目录下
麻烦再帮忙回复下,谢谢啦
admin .php代码如下:
<?php
if($_POST){
$name = $_POST['txtName'];
if ($name){
header("location:admin.php?act=index");
}
}
?>
<html>
<head>
<title></title>
<script type="text/javascript" src = "mylogin.js"></script>
<script type="text/javascript"></script>
</head>
<body onLoad="document.getElementById('txtName').focus()">
<span style="color: #FFF"></span>
<table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;">
<tr>
<td valign="middle">
<div class="form">
<form method="post" enctype="application/x-www-form-urlencoded" action=admin.php>
<table border="1" align="right" cellpadding="1" cellspacing="1">
<tr>
<td width="68" class="labName">用户名</td>
<td><input type="text" name="txtName" id="txtName"></td>
</tr>
<tr>
<td class="labPWD">密码</td>
<td><input type="password" name="txtPwd" id="txtPwd"></td>
</tr>
<tr>
<td height="45" colspan="3" align="center" valign="middle"><input type="submit" src="image/admin/login/btnsubmit.jpg"></td>
</tr>
</table>
</form>
</div>
</div></td>
</tr>
</table>
</body>
</html>
@xuzuning 谢谢回复
另外 我改为这样也不行的,admin.php 和index.php是同一目录下。 麻烦再帮忙回复下 ,谢谢啦
<?php
$name = $_POST['txtName']; if($name) {
if($_GET['act']=="index"){
header("location:admin.php?act=index");
}
}
?><html>
<head>
<title></title>
<script type="text/javascript" src = "mylogin.js"></script>
<script type="text/javascript"></script>
</head>
<body onLoad="document.getElementById('txtName').focus()">
<span style="color: #FFF"></span>
<table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;">
<tr>
<td valign="middle">
<div class="form">
<form method="post" enctype="application/x-www-form-urlencoded" action=?act=index>
<table border="1" align="right" cellpadding="1" cellspacing="1">
<tr>
<td width="68" class="labName">用户名</td>
<td><input type="text" name="txtName" id="txtName"></td>
</tr>
<tr>
<td class="labPWD">密码</td>
<td><input type="password" name="txtPwd" id="txtPwd"></td>
</tr>
<tr>
<td height="45" colspan="3" align="center" valign="middle"><input type="submit" src="image/admin/login/btnsubmit.jpg"></td>
</tr>
</table>
</form>
</div>
</div></td>
</tr>
</table>
</body>
</html>
@amodernpeople 谢谢您的回复麻烦再详细说下吧,我不太明白您的意思
我是这么处理的,但是还是不行。
admin.php的代码:
<?php
$name = $_POST['txtName']; if($name) {
if($_GET['act']=="index"){
header("location:admin.php?act=index");
}
}
?><html>
<head>
<title></title>
<script type="text/javascript" src = "mylogin.js"></script>
<script type="text/javascript"></script>
</head>
<body onLoad="document.getElementById('txtName').focus()">
<span style="color: #FFF"></span>
<table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;">
<tr>
<td valign="middle">
<div class="form">
<form method="post" enctype="application/x-www-form-urlencoded" action=?act=index>
<table border="1" align="right" cellpadding="1" cellspacing="1">
<tr>
<td width="68" class="labName">用户名</td>
<td><input type="text" name="txtName" id="txtName"></td>
</tr>
<tr>
<td class="labPWD">密码</td>
<td><input type="password" name="txtPwd" id="txtPwd"></td>
</tr>
<tr>
<td height="45" colspan="3" align="center" valign="middle"><input type="submit" src="image/admin/login/btnsubmit.jpg"></td>
</tr>
</table>
</form>
</div>
</div></td>
</tr>
</table>
</body>
</html>
当提交表单,表单action包含get,在post的情况下,拿到get,然后跳转到包含get的url。
跳转后,post已经不存在,拿不到get,还是回到开始。
在拿到get的情况下,你做跳转,跳的还是本身页面,参数一样,什么时候才能出这个圈?
如果post不存在的情况下,还是可以拿到get的话,进行跳转,就是个无限循环。你有两个文件,可是你的index.php何时才登场?
@ lanshs 谢谢您的回复,
请问应该具体怎么修改呢? 麻烦再帮忙回复,谢了
<?php
//初始判断 这里用cookie
//如果cookie没有,看看有没有post数据,有的话写cookie,然后做个跳转到get index。post没有的话,就显示本页面了,也就是登录表单。
//如果cookie有,不管任何post数据,看看get是什么,就加载什么页面。给个默认为indexif(!$_COOKIE['name']) {
if($_POST['name']) {
setcookie('name', $_POST['name']);
header("location:admin.php?act=index");
}
}else{
$login = true;
}//不仅仅是index,可以设置别的
if($login) {
if($_GET['act'] == 'index' || !$_GET['act']) {
require 'index.php';
exit;
}
if($_GET['act'] == 'test') {
require 'test.php';
exit;
}
}?>html
from action='?'
.......
给你加了cookie,你也可以不用,不用的话,怎么做判断自己考虑。
主要是,别兜圈子
代码直接手打,不完整,未经过测试
header("location:admin.php?act=index");
改为
require 'index.php';exit;圈子跳出来,但后续没了。因为你的每一个请求都必须post你的登录信息,否则打开的就是登录窗口。
@lanshs 谢谢您的回复
修改后的 admin.php代码如下:但是代码的整个流程我有点疑问,麻烦指出我的理解在哪里错了。1 刚开始在浏览器输入 localhost/mytry/admin.php,运行后,
先跑 var_dump($_COOKIE['TextName']); 打印出: D:\Apache24\htdocs\mytry\admin.php:3:null
然后 弹出:_COOKIE[TextName] is null
接着就显示了 登录的界面2 当输入 用户名 和密码后,点击 提交按钮, 这时候,
跑 var_dump($_COOKIE['TextName']),打印出: D:\Apache24\htdocs\mytry\admin.php:3:string 'admin' 为什么会打印出 admin呢? 毕竟 程序运行到这里 并没有运行 setcookie('TextName', $_POST['TextName']);为什么会有值呢?3 接着 会运行:echo "<script>alert('login=true');</script>"; 此时 弹出: login=true4 接着就会跑:echo "<script>alert('get');</script>"; 此时 弹出: get最后 就会跳转到 index.php,显示该页面的信息。
请问 : 程序 始终没有 运行echo "<script>alert('cookie');</script>"; 这句代码,也就是 没有运行setcookie('TextName', $_POST['TextName']);这句代码, 那么为什么输入用户名和密码后,点击登录 后, var_dump($_COOKIE['TextName']), 会有值的呢?麻烦再帮忙回复,谢谢啦
<?php var_dump($_COOKIE['TextName']);
if(!$_COOKIE['TextName']) {
echo "<script>alert('_COOKIE[TextName] is null');</script>";
if($_POST['TextName']) {
echo "<script>alert('cookie');</script>";
setcookie('TextName', $_POST['TextName']);
header("location:admin.php?act=index");
}
}else{
echo "<script>alert('login=true');</script>";
$login = true;
}
//不仅仅是index,可以设置别的
if($login) {
if($_GET['act'] == 'index' || !$_GET['act']) {
echo "<script>alert('get');</script>";
require 'index.php';
exit;
}
if($_GET['act'] == 'test') {
require 'test.php';
exit;
}
}
?>
<html>
<head>
<title></title>
<script type="text/javascript" src = "mylogin.js"></script>
<script type="text/javascript"></script>
</head>
<body onLoad="document.getElementById('txtName').focus()">
<span style="color: #FFF"></span>
<table border="1" align="center" cellpadding="1" cellspacing="1" height="100%" style="margin:0 auto;">
<tr>
<td valign="middle">
<div class="form">
<form method="post" enctype="application/x-www-form-urlencoded" action=?act=index>
<table border="1" align="right" cellpadding="1" cellspacing="1">
<tr>
<td width="68" class="labName">用户名</td>
<td><input type="text" name="TextName" id="TextName"></td>
</tr>
<tr>
<td class="labPWD">密码</td>
<td><input type="password" name="txtPwd" id="txtPwd"></td>
</tr>
<tr>
<td height="45" colspan="3" align="center" valign="middle"><input type="submit" src="image/admin/login/btnsubmit.jpg"></td>
</tr>
</table>
</form>
</div>
</div></td>
</tr>
</table>
</body>
</html>
@ lanshs 谢谢您的回复想请问下,在我的原来代码里边,以下所示,为什么当$_GET['act']=="index"的时候,执行header("location:admin.php?act=index");。 也就是跳转到 index.php的页面。而您说 “跳转后,post已经不存在,拿不到get,还是回到开始。"这句话我不太理解。 为什么跳转后,还需要 post呢? 而这里的post只的是 post上来的用户名 和密码吧,但是跳转后为什么还需要要这个用户名和密码呢? 也就是说 是不是由于调转到的页面 还是需要用户名和密码的,所以就跳转失败呢?麻烦再详细说明下,谢谢啦
admin.php的代码:
<?php
$name = $_POST['txtName']; if($name) {
if($_GET['act']=="index"){
header("location:admin.php?act=index");
}
}
?><html>
<head>
<title></title>
<script type="text/javascript" src = "mylogin.js"></script>
<script type="text/javascript">
。。
<?php
var_dump(@$_COOKIE['TextName']);
if(!@$_COOKIE['TextName']) {
echo "<script>alert('_COOKIE[TextName] is null');</script>";
if(@$_POST['TextName']) {
echo "<script>alert('cookie');</script>";
setcookie('TextName', $_POST['TextName']);
header("location:test.php?act=index");
}
}else{
echo "<script>alert('login=true');</script>";
$login = true;
}
if(@$login) {
if(@$_GET['act'] == 'index' || !@$_GET['act']) {
echo "<script>alert('get');</script>";
require 'index.php';
exit;
}
if(@$_GET['act'] == 'test') {
require 'test.php';
exit;
}
}
?>
我试了一下,给你添加几个@符号,取消报错。cookie或者GET的值不存在就报错,不过还是正常运行的。报错不好看。你测试的第一步正常,第二步出现不明白的地方。
其实你的第一步到第二步之间还有过程
一、没有cookie,显示表单(这是你的第一步,没有问题)
二、提交表单后,检查到有post,进行setcookie,然后跳转到admin.php?act=index
三、检测到cookie存在了。。
你的第二步在这里是第三步,只不过,你打印的js弹出,这里第二步里,不起作用了,因为进行了header("location:admin.php?act=index");
连续过程就是,打开页面,填写表单,提交,setcookie,header跳转,已经存在cookie,接收get,require打开get act的对应页面。
提交表单到header跳转之间,因为弹窗没了,你以为只是一次请求,事实上,两次请求了,第一次请求有setcookie,第二次请求,一开始就存在cookie。
这样,你的疑惑应该解释的很清楚了。你的步骤最后:“最后 就会跳转到 index.php,显示该页面的信息。”
这个不是跳转,require吧index.php包含到这个页面了。包含之后,进行exit,防止后续代码进行干扰。
<?php if($_GET['act']=="index"){
header("location:admin.php?act=index");
}?>
这样,没有post的干扰,这个过程就是个循环
header打开的还是admin.php,参数还是index,还是会运行header,然后又打开admin.php
当你加上post,只有post存在,才会运行这个
那么
你打开admin.php,post不存在,显示表单
当你提交表单,post存在,取GET然后header
header就是把地址改为xxx,打开xxx网页,这个打开就是GET请求,没有post
header的location是admin.php,打开的是admin.php,post不存在,显示表单
你看这不就是兜圈了
问号后边的参数,你不获取的话,有也不会起任何作用。当post不存在,act参数不起任何作用,相当于没有