允许脚本与SESSION身份验证一起工作


Allowing script to function with SESSION authentication

我有一个php脚本,如果进入文本框表单的条目与数据库中的任何内容匹配,那么创建一个会话:

 $name= $_POST['name']; 
 $db= new  PDO('sqlite:people.db'); 
 $query = "SELECT * from names WHERE name= '$name'";
 $results = $db->Query($query)->fetchAll(); 
 $allow= count($results); 
 if ( $table  > 0 )
    {
    session_start();
    $enable= 1;
    $_SESSION['enabled'] = $enable;
    }

另一个脚本如下:

 if (isset($_SESSION))
{
 $code= $_POST['code']; 
 $db= new  PDO('sqlite:Properties1.db'); 
 $query = "SELECT * from properties WHERE postcode = '$postcode'";
 $results = $db->Query($query)->fetchAll(); //fetchAll puts results into an array
  $allow= count($results); //count the rows in the results array
 if ( $rowcount > 0 )
 {
 do this
 }

但是第二个脚本没有获取会话

if (isset($_SESSION)) this将始终返回1并且if条件将始终执行。因为$_SESSION超全局,并且它总是在整个脚本中设置。

试着检查一下,

if (isset($_SESSION['enabled']))
{