所以我的php登录脚本有一个愚蠢的问题。我注意到当我尝试使用它并登录时在login.php上设置会话变量但是当我尝试移动页面时它会破坏它。所以在login.php上我检查是否有用户名和密码与数据库行匹配,如果匹配则设置一个变量告诉用户登录用户。然后检查站点旁边的变量,如果它被设置我让他们查看网站的内侧如果没有设置我踢他们。
所以这是我的login.php
<?php
include 'functions/functions.php';
$db = mysqlconnect();?>
<?php
error_reporting(0);
session_start();
?><?php
if (isset($_POST["Submit"])) {
$password = mysql_real_escape_string($_POST['password']);
$password2 = strip_tags($password);
$md5password = md5($password2);
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ? ");
$statement->execute(array($_POST['username'],$md5password));
$count = $statement->rowCount();
/// If usernam and password match we carry on
if ($count == "1")
{
$username23 = mysql_real_escape_string($_POST['username']);
$thereusername = strip_tags($username23);
$_SESSION['username'] = $thereusername ;
echo "You are now being logged in";
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
$result5534453465 = mysql_query("UPDATE users SET lastip='".$ip."' WHERE username='".$_SESSION['username']."'")
or die(mysql_error());
mysql_query("INSERT INTO user_log (username, ip)
VALUES ('".$_SESSION['username']."', '".$ip."')");
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=home.php">';
exit;
}else{
echo "Password or username is wrong";
}
}
?>
<form name="input" action="" method="post">
<p>Username:</p>
<p>
<input type="text" name="username" id ="username">
</p>
<p>Password: </p>
<p>
<input type="password" name="password" id = "password">
</p>
<p>
<input type="submit" value="Submit" name = "Submit">
</p>
</form>
我已经尝试回应$ _SESSION ['用户名'],因为用户被重定向到网站并且回声正确,因此设置正确....
然后在网站的内侧,我检查是否正在设置$ _SESSION ['用户名']
ini_set('session.cookie_httponly',true);
session_start();
if(isset($_SESSION['last_ip']) == false){
$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
}
if ($_SESSION['last_ip'] !== $_SERVER['REMOTE_ADDR']){
session_unset();
session_destroy();
}
if(empty($_SESSION['username'])){
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
exit;
}
由于某种原因,该页面说用户名是空的...有趣的是,如果我注销破坏整个会话然后尝试登录它将工作....
编辑:我刚刚编辑了这段代码
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
要
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$_SESSION['last_ip']=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$_SESSION['last_ip']=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$_SESSION['last_ip']=$_SERVER['REMOTE_ADDR'];
}
现在一切似乎都有效......我在猜测
if(isset($_SESSION['last_ip']) == false){
$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
}
if ($_SESSION['last_ip'] !== $_SERVER['REMOTE_ADDR']){
session_unset();
session_destroy();
}
杀了会议?
另一个编辑: 我刚刚注意到上述编码之后,登录现在适用于firefox而不是chrome,会话变量在登录后移动到不同的页面后没有在chrome中设置。我想也许是主机问题?我使用的是PHP 5.2版本,我已升级到5.3但仍有问题...
侧面php.in 中的会话设置
ssion
Session Support enabled
Registered save handlers files user sqlite
Registered serializer handlers php php_binary wddx
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 Off Off
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly On Off
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 5 5
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path no value no value
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0
答案 0 :(得分:0)
在初始化会话之前,您的脚本不应打印任何内容,甚至不能打印单个空格或换行符。
<?php // whitespaces at the beginning
include 'functions/functions.php';
$db = mysqlconnect();?> // linebreak + spaces between the closing tag and the following opening tag
<?php
error_reporting(0);
session_start();
另请检查您的functions.php
文件。 Per php.net:“如果文件是纯PHP代码,最好省略文件末尾的PHP结束标记。这可以防止在PHP结束标记之后添加意外的空格或新行,这可能会导致不需要的效果,因为当程序员无意在脚本中发送任何输出时,PHP将启动输出缓冲。“
答案 1 :(得分:-1)
在ini_set函数的开头,您需要将$ _SESSION声明为GLOBAL变量。
EG:
function ini_set() {
GLOBAL $_SESSION ;
...