我已经为我的网站创建了一个正常工作的登录页面,但是如果我只是在网址后输入网页,我甚至无需登录即可访问它!
我需要它,以便您只能在登录时访问页面。
所以我的登录页面使用的表单又使用了检查登录脚本。
<?php
$host="localhost"; // Host name
$username="blank"; // Mysql username
$password="blank"; // Mysql password
$db_name="blank"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
Print "<html>";
Print "<head>";
Print "<link href='styles.css' rel='stylesheet' type='text/css' />";
Print "</head>";
Print "<body>";
Print "<img src=images/banner2.png alt='banner' />";
Print "<p>Wrong Username or Bad Password.</p>";
Print "<p>Or <a href='./index.html'>Click Here</a> to try again.</p>";
Print "</body>";
Print "</html>";
}
?>
在每个页面上我都有:
<?php
session_start();
if(!session_is_registered(myusername)){
header("location: index.html");
}
?>
密码表单如果您没有有效的密码然后输入错误的密码就有效,但如果我知道他们的姓名,我仍然可以访问每一页。
感谢
答案 0 :(得分:3)
直接引用$ _SESSION变量,
半假:
if(authorized()){
$_SESSION['username'] = 'Hi';
}
if($_SESSION['username'] != ''){
// online
}
答案 1 :(得分:2)
此功能已被弃用为 PHP 5.3.0。依靠这个功能 非常沮丧。
你应该在下面使用写代码
<?php
if($count==1){
session_start();
$_SESSION['$myusername'];
$_SESSION['$mypassword'];
header("location:login_success.php");
}
else {
session_unset();
session_destroy();
$_SESSION = array();
session_start();
}
?>
并在每个页面上查看
$authorized=false;
@session_start();
if(isset($_SESSION['$myusername'] && isset($_SESSION['$mypassword']);
{
$authorized = true;
}
if(!authorized)
{
header('location:index.html');
exit();
}
答案 2 :(得分:0)
如果密码无效,您是否尝试过使用session_destroy?