PHP页面跳转以及伪登录实例
PHP页面跳转一、header()函数
header()函数是PHP中进行页面跳转的一种十分简单的方法。header()函数的主要功能是将HTTP协议标头(header)输出到浏览器。
header()函数的定义如下:
void header (string string [,bool replace [,int http_response_code]])
可选参数replace指明是替换前一条类似标头还是添加一条相同类型的标头,默认为替换。
第二个可选参数http_response_code强制将HTTP相应代码设为指定值。 header函数中Location类型的标头是一种特殊的header调用,常用来实现页面跳转。
注意:
1.location和“:”号间不能有空格,否则不会跳转。
2.在用header前不能有任何的输出。
3.header后的PHP代码还会被执行。例如,将浏览器重定向到百度
代码如下:
< ?php
//重定向浏览器
header("Location: http://www.baidu.com ");
//确保重定向后,后续代码不会被执行
exit;
?>
PHP页面跳转二、Meta标签
Meta标签是HTML中负责提供文档元信息的标签,在PHP程序中使用该标签,也可以实现页面跳转。 若定义http-equiv为refresh,则打开该页面时将根据content规定的值在一定时间内跳转到相应页面。
若设置content="秒数;url=网址",则定义了经过多长时间后页面跳转到指定的网址。例如,使用meta标签实现疫苗后页面自动跳转到js代码官网。
代码如下:
< meta http-equiv = "refresh"
content = "1;url=http://www.jsdaima.com/" >
例如,以下程序meta.php实现在该页面中停留一秒后页面自动跳转到http://www.sina.com/。
代码如下:
< ?php
$ url = "http://http://www.sina.com/" ; ?>
< html >
< head >
< meta http-equiv = "refresh" content ="1;
url = < ?php echo $url; ?> " >
< /head >
< body >
页面只停留一秒……
< /body >
< /html
PHP页面跳转三、JavaScript
例如,此代码可以放在程序中的任何合法位置。
代码如下:
< ?php
$ url = "http://bbs.lampbrother.net" ;
echo " < script language = 'javascript'
type = 'text/javascript' > ";
echo " window.location.href = '$url' ";
echo " < /script > ";
?>
PHP代码实现伪登录页面跳转:
test05.PHP代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Examples</title>
<style>
div{
margin: auto;
text-align: center;
}
</style>
</head>
<body style=" margin: auto;">
<div>
<form action="test06.php" method="post">
<p>账号: <input type="text" name="uname"></p>
<p>密码: <input type="password" name="pwd"></p>
<p>
<input type="submit" value="登录">
<input type="submit" value="注册">
</p>
</form> </div> </body>
</html>
test06.PHP代码:
<?php
header('ContentType=text/html charset=utf-8'); //文档声明
?> <?php
//判断接受数据是否为空
if (empty($_POST['uname']) || empty($_POST['pwd'])) {
//重定向(若传输数据为空则返回登录页)
header('location:http://localhost/test/test05.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Examples</title> <style>
/* 简单页面布局 */
div{
width: 1200px;
margin: auto;
border: 1px solid #ccc;
height: 50px;
background-color: #CCC; }
span{
float: right;
margin-right:50px;
font-size: 14px;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<div>
<span>
<?php
//输出用户名
echo '账户: '.($_REQUEST['uname']);
?>
</span> </div>
</body>
</html>
效果如下图所示:
最新文章
- PHP 基础
- jquery特效收藏
- 【python】getopt使用
- Docker 使用指南 (一)—— 基本操作
- 在Excel中引用其他宏
- PHP 代码质量检测工具的安装与使用
- Mysql 储存过程以及 python callproc调用
- .net转php laraval框架学习系列(四) 项目实战---View
- Java提高学习之Object(3)
- json中关于jo.[]中字符串一致的问题
- hdu 2159 FATE (二维完全背包)
- poj-2115-exgcd
- 前端模拟后台返回数据之Mockjs
- 【python】基础入门
- AngularJS 初始化加载流程
- VS2010/MFC编程入门之二十一(常用控件:编辑框Edit Control)
- ESXi到KVM之v2v迁移
- Android -- ListView与ArrayAdapter、SimpleAdapter
- canvas 绘制环形进度条
- ipfs01
热门文章
- sqlalchemy 连接mysql8.0报 RuntimeError: cryptograpy si requeired for sha256_password 错误
- 深度解析Java可变参数类型以及与数组的区别
- iOS应用的语言设置
- 【快学springboot】9.使用 @Transactional 注解配置事务管理
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:选择框(Select)
- LeetCode刷题笔记和想法(C++)
- [ERROR] error: error while loading <;root>;, error in opening zip file error: scala.reflect.internal.MissingRequirementError: object scala.runtime in compiler mirror not found.
- MariaDB——数据库基础与sql语句
- JQuery--事件处理
- SpringBoot 入门demo