Php 未保存会话

Php 未保存会话,php,pdo,Php,Pdo,所以我的php登录脚本有一个愚蠢的问题。我注意到,当我尝试使用它并登录时,它会在login.php上设置会话变量,但当我尝试移动页面时,它会破坏它。因此,在login.php上,我检查用户名和密码是否与数据库行的匹配,如果匹配,我将设置一个变量,告诉站点用户已登录。然后我检查站点中的变量,如果设置了,我让他们查看站点中的变量,如果没有设置,我踢他们 这是我的login.php <?php include 'functions/functions.php'; $db = my

所以我的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> 

现在一切似乎都在运转。。。。我在猜答案

 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,但仍然存在问题

side 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

在ini_set函数的开头,您需要将$会话声明为全局变量

例如:


在初始化会话之前,脚本不应打印任何内容,甚至不应打印单个空格或换行符

      <?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();
//换行符+结束标记和以下开始标记之间的空格

您的登录页面上有
会话\u start
吗?是的,在db connect的顶部,我将立即编辑代码以显示此类问题(“我有一堆代码,它不工作”)被称为“太本地化”,因此,不幸的是,不欢迎在此网站上使用。我正要建议
echo
if($\u会话)中使用['last\u ip']!=$\u服务器['REMOTE\u ADDR'])
block查看它是否每次都显示。很高兴你解决了这个问题,尽管我刚刚注意到另一次编辑它只对firefox起作用,而对chrome不起作用…变量仍然没有为chrome设置。只有firefox$\u会话已经是一个超全局会话。ini\u集是一个PHP函数,而不是用户创建的函数。i现在已经进行了编辑,因此会话开始是针对inculde的,但它仍然允许我通过firefox登录,但不允许我登录到任何其他broswer。我的主机告诉我需要设置会话超时,这就是它无法工作的原因。。
 if(isset($_SESSION['last_ip']) == false){
$_SESSION['last_ip'] = $_SERVER['REMOTE_ADDR'];
}

if ($_SESSION['last_ip'] !== $_SERVER['REMOTE_ADDR']){

session_unset();
session_destroy();

}
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
function ini_set() {
    GLOBAL $_SESSION ;
...
      <?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();