我正在尝试为我的网页添加一些安全性。
我的网页"page.html"有很多javascript脚本和html代码。我可以将扩展名更改为 php。我对此没有问题。
我正在使用我在互联网上找到的示例代码来了解session_start的工作原理。如果我有 html 扩展名,它可以正常工作,但即使我不添加用户并通过,我也可以访问该网页。 如果我使用 PHP 扩展,网页会再次将我扔到登录 promt。使用php扩展名,当我在浏览器上编写网页时,我无法访问该网页。
这是我检查访问权限的代码:检查访问.php
<?php
session_start();
//check the autenticated value
if ($_SESSION["autenticado"] != "si") {
//if doesn't exits, goes to login webpage
header("Location: login.php");
exit();
}
?>
我在 html 和 php 网页上添加了这一行:
<?php include "checkAccess.php";?>
我知道它有效,因为我可以用 html 格式访问我的页面。但是当我将扩展名更改为 php 时,它不起作用。
我必须在 php.ini 或类似的东西中配置任何值吗?
我添加我的其他 php 文件来检查会话:
自闭.php:
<?
session_start();
if ($_POST["usuario"]=="user" && $_POST["contrasena"]=="123456"){
$_SESSION["autenticado"]= "SI";
header ("Location: mypage.php");
}else {
header("Location: login.php?errorusuario=si");
}
?>
登录.php:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Autenticación PHP</title>
</head>
<body>
<h1>Formulario de autenticación</h1>
<?php if(isset($_GET['errorusuario'])){if ($_GET['errorusuario']=="si"){?>
<font color="red"><b>Datos incorrectos</b></font>
<?php }}else{?>
Introduce tu nombre de usuario y contraseña
<?}?>
<form action="autenticacion.php" method="POST">
<table border="0">
<tr><td>Nombre de usuario:</td><td><input name="usuario" size="25" value=""/></td></tr>
<tr><td>Contraseña:</td><td><input name="contrasena" size="25" type="password"/> </td></tr>
<tr><td/><td><input type="submit" value="Inicio de sesión"/></td></tr>
</table>
</form>
Para ingresar, débes ingresar <b>pablo</b> en el 1er campo y <b>123456</b> en el 2do.
</body>
</html>
我正在使用 xampp 来测试页面。
问:$_SESSION["autenticado"]
设置在哪里?
问:你在那里也做过session_start()
吗? 您必须在两个页面中调用start_session((。
问:如果echo
`$_SESSION["autenticado"]
的价值,您会看到什么? 为什么不对整个 $_SESSION[] 数组做一个 var_dump((,看看那里有没有任何东西呢?
问:您是否在浏览器中启用了 Cookie?
这里有一个关于PHP会话的好教程:
- http://www.w3schools.com/php/php_sessions.asp
以下是 $_SESSION[] 超全局的文档(请注意,您可能应该在代码中使用"isset($_SESSION("函数(:
- http://php.net/manual/en/reserved.variables.session.php
是的,php.ini中有会话设置。 但坦率地说,我不会惹他们中的任何一个,除非作为最后的手段,除非你确切地知道你在做什么。 以下是文档:
- http://php.net/manual/en/session.configuration.php