我想包含一个PHP页面来保护每一个页面,在你登录后,它会显示你的用户名在右上角,
它还限制了登录用户提供的唯一数据,如他的简历和个人信息,但没有看到其他用户的信息。
结构将与上一篇文章
相同index.php(包括header.php, content.php和footer.php)
header.php的标题将在用户登录后更改菜单
谢谢。
对
安迪如果你想要一个脚本来确保它是这样的:
first:假设你没有使用设计模式,它是一个php基础项目,原型类似"scripts(folder) css(folder) js(folder) index.php header.php and footer.php"。让我们创建一个"security.php"
<?php
session_start(); //starting session to acces to it
if(empty($_SESSION["username"])){// if there's no data username in session
header("location: ./login.php"); //go and take them out of here!
}
?>
现在你已经准备好了"security.php",你只需要把它包含到你的保护页面中,并创建一个"login.php"页面。
示例:用于包含安全性
<?php
//@mySecurePage
include "security.php";
//My Page Content Code or event header code if you want validation after loading anything (Best)
?>
第二步:创建一个类似于。
的登录页面<?php
if(!empty($_POST["username"]) && !empty($_POST["password"])){// if all data from login was sent
if($_POST["username"] == "me" && $_POST["password"] == 1234){//your validations here
session_start();//start session to acces it
$_SESSION["username"] == $_POST["username"];//allocate username
header("location: ./securedPageHere.php");//forward to a securedPage
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Login Page</title>
</head>
<body>
<form class="" action="" method="post"><!-- action is empty to send data to same page (login)-->
<input type="text" name="username" value="" placeholder="username">
<input type="password" name="password" value="" placeholder="password">
<input type="button" name="login" value="Login">
</form>
</body>
</html>
现在我们差不多完成了,只需要在菜单栏加载用户名
第三:在菜单栏加载用户名。只需添加access session值(记住,如果你加载了"security.php",你已经启动了session
)<div><?php print($_SESSION["username"]); ?></div>
现在要使用Logut Button,您必须销毁会话,因此向其添加侦听器,然后只需执行如下脚本:
<?php
unset($_SESSION); //clear session array
session_destroy(); //Destroy session
?>
希望对你有帮助。
编辑:限制数据访问只使用用户名(最佳id)数据从登录验证数据保存在会话数组:)。