Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP登录的奇怪行为_Php_Login - Fatal编程技术网

PHP登录的奇怪行为

PHP登录的奇怪行为,php,login,Php,Login,这更像是一个调试问题,而不是一个实际问题。我有一个PHP登录脚本,它应该检查本地数据库中的用户信息,如果存在,则显示它们。或者,将它们重定向到GoogleOAuth2登录过程。以下php文件涉及登录流: google_login.php <?php error_reporting(E_ALL); ini_set('display_errors', 1); require('http.php'); require('oauth_client.php'); require('../config

这更像是一个调试问题,而不是一个实际问题。我有一个PHP登录脚本,它应该检查本地数据库中的用户信息,如果存在,则显示它们。或者,将它们重定向到GoogleOAuth2登录过程。以下php文件涉及登录流:

google_login.php

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require('http.php');
require('oauth_client.php');
require('../config.php');
require('StructuredQuery.php');

define("SCOPE", 'https://www.googleapis.com/auth/userinfo.email '.
        'https://www.googleapis.com/auth/userinfo.profile' );

$client = new oauth_client_class;
$sq= new StructuredQuery();

// set the offline access only if you need to call an API
// when the user is not present and the token may expire
$client->offline = FALSE;

$client->debug = false;
$client->debug_http = true;
$client->redirect_uri = GOOGLE_REDIRECT_URL;

$client->client_id = GOOGLE_CLIENT_ID;
$application_line = __LINE__;
$client->client_secret = GOOGLE_CLIENT_SECRET;

if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
  die('Please go to Google APIs console page ' .
          'http://code.google.com/apis/console in the API access tab, ' .
          'create a new client ID, and in the line ' . $application_line .
          ' set the client_id to Client ID and client_secret with Client Secret. ' .
          'The callback URL must be ' . $client->redirect_uri . ' but make sure ' .
          'the domain is valid and can be resolved by a public DNS.');

/* API permissions
 */
$client->scope = SCOPE;
if (($success = $client->Initialize())) {
  if (($success = $client->Process())) {
    if (strlen($client->authorization_error)) {
      $client->error = $client->authorization_error;
      $success = false;
    } elseif (strlen($client->access_token)) {
      $success = $client->CallAPI(
              'https://www.googleapis.com/oauth2/v1/userinfo', 'GET', array(), array('FailOnAccessError' => true), $user);
    }
  }
  $success = $client->Finalize($success);
}
if ($client->exit)
  exit;
if ($success) {
  // Now check if user exist with same email ID
  try {
    $result = $sq->getUserInfo($user->id);

    if ($result["count"] > 0) {
      // User Exist 
            $_SESSION["name"] = $result["name"];
            $_SESSION["email"] = $result["email"];
            $_SESSION["clevel"]=$result["clevel"];
            $_SESSION["new_user"] = "no";
    } else {
      // New user, Insert in database
      $result = $sq->putNewUserInfo($user->id,$user->name,$user->email);
      if ($result===true) {
        $_SESSION["name"] = $user->name;
        $_SESSION["email"] = $user->email;
        $_SESSION["new_user"] = "yes";
        $_SESSION["e_msg"] = "";
      }
    }

    $_SESSION["login_type"]="Google";
  } catch (Exception $ex) {
    $_SESSION["e_msg"] = $ex->getMessage();
  }>

  $_SESSION["user_id"] = $user->id;
} else {
  $_SESSION["e_msg"] = $client->error;
}
header("Location: ".ROOT_DIR."homepage.php");
exit;
?>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require_once 'config.php';
class StructuredQuery{
    var $opt;
    var $pdo;
function __construct(){
    $opt = [
    PDO::ATTR_PERSISTENT         => FALSE,
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    $this->pdo = new PDO(DB_DRIVER.":host=".DB_SERVER.";dbname=".DB_NAME, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, $opt);
}
// Cross Site Script  & Code Injection Sanitization
function xss_cleaner($input_str) {
    $return_str = str_replace( array('<',';','|','&','>',"'",'"',')','('), array('&lt;','&#58;','&#124;','&#38;','&gt;','&apos;','&#x22;','&#x29;','&#x28;'), $input_str );
    $return_str = str_ireplace( '%3Cscript', '', $return_str );
    return $return_str;
}
//SQLInjection detect
function sql_injection_detect($input_query){
    try{
        $blacklist=array('SELECT','WHERE','UPDATE','DELETE','INSERT','FROM','DROP','MERGE','SET','INSERT','REMOVE','REPLACE','QUERY');
        $err_level=0;
        foreach($blacklist as $blacklist_item){
            if(stripos($input_query,$blacklist_item)!==false){
                $err_level++; //Counter for number of blacklist words used. 2 means dangerous. Terminate immediately.
                if($err_level==2){
                    die('Was that an IT joke? Cause I am a 12th grader, not an IT Pro.');
                }
            }
        }
        return true;
    }catch(Exception $e){
            echo 'Exception Occured:',$e->getMessage(),"\n";
            die('You\'ve been Terminated');
        }
}
function getUserInfo($user_id){
    $user_id=xss_cleaner($user_id);
    if(sql_injection_detect($user_id)){
        $query=$pdo->prepare("select statement");
        $query->bindParam(":user_id",$user_id,PDO::PARAM_STR);
        $query->execute();
        $result=$query->fetch();
        $result["count"]=$query->rowCount();
        return $result;
    }
}
function putNewUserInfo($user_id,$name,$email){
    $user_id=$this->xss_cleaner($user_id);
    $name=xss_cleaner($name);
    $email=xss_cleaner($email);
    if(sql_injection_detect($user_id) && sql_injection_detect($name) && sql_injection_detect($email)){
        $query=$pdo->prepare("insert statement");
        $query->bindParam(":user_id",$user_id,PDO::PARAM_STR);
        $query->bindParam(":name",$name,PDO::PARAM_STR);
        $query->bindParam(":email",$email,PDO::PARAM_STR);
        $query->execute();
        return true;
    }else{
        return false;
        }
}
function modifyUserInfo($user_id,$name,$email,$clevel){
    $user_id=xss_cleaner($user_id);
    $name=xss_cleaner($name);
    $email=xss_cleaner($email);
    $clevel=xss_cleaner($clevel);
    if(sql_injection_detect($user_id) && sql_injection_detect($name) && sql_injection_detect($email) && sql_injection_detect($clevel)){
        $query=$pdo->prepare("update statement");
        $query->bindParam(":user_id",$user_id,PDO::PARAM_STR);
        $query->bindParam(":name",$name,PDO::PARAM_STR);
        $query->bindParam(":email",$email,PDO::PARAM_STR);
        $query->bindParam(":clevel",$clevel,PDO::PARAM_INT);
        $query->execute();
        return true;
    }else{
        return false;
        }
}
}

StructuredQuery.php

<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require('http.php');
require('oauth_client.php');
require('../config.php');
require('StructuredQuery.php');

define("SCOPE", 'https://www.googleapis.com/auth/userinfo.email '.
        'https://www.googleapis.com/auth/userinfo.profile' );

$client = new oauth_client_class;
$sq= new StructuredQuery();

// set the offline access only if you need to call an API
// when the user is not present and the token may expire
$client->offline = FALSE;

$client->debug = false;
$client->debug_http = true;
$client->redirect_uri = GOOGLE_REDIRECT_URL;

$client->client_id = GOOGLE_CLIENT_ID;
$application_line = __LINE__;
$client->client_secret = GOOGLE_CLIENT_SECRET;

if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
  die('Please go to Google APIs console page ' .
          'http://code.google.com/apis/console in the API access tab, ' .
          'create a new client ID, and in the line ' . $application_line .
          ' set the client_id to Client ID and client_secret with Client Secret. ' .
          'The callback URL must be ' . $client->redirect_uri . ' but make sure ' .
          'the domain is valid and can be resolved by a public DNS.');

/* API permissions
 */
$client->scope = SCOPE;
if (($success = $client->Initialize())) {
  if (($success = $client->Process())) {
    if (strlen($client->authorization_error)) {
      $client->error = $client->authorization_error;
      $success = false;
    } elseif (strlen($client->access_token)) {
      $success = $client->CallAPI(
              'https://www.googleapis.com/oauth2/v1/userinfo', 'GET', array(), array('FailOnAccessError' => true), $user);
    }
  }
  $success = $client->Finalize($success);
}
if ($client->exit)
  exit;
if ($success) {
  // Now check if user exist with same email ID
  try {
    $result = $sq->getUserInfo($user->id);

    if ($result["count"] > 0) {
      // User Exist 
            $_SESSION["name"] = $result["name"];
            $_SESSION["email"] = $result["email"];
            $_SESSION["clevel"]=$result["clevel"];
            $_SESSION["new_user"] = "no";
    } else {
      // New user, Insert in database
      $result = $sq->putNewUserInfo($user->id,$user->name,$user->email);
      if ($result===true) {
        $_SESSION["name"] = $user->name;
        $_SESSION["email"] = $user->email;
        $_SESSION["new_user"] = "yes";
        $_SESSION["e_msg"] = "";
      }
    }

    $_SESSION["login_type"]="Google";
  } catch (Exception $ex) {
    $_SESSION["e_msg"] = $ex->getMessage();
  }>

  $_SESSION["user_id"] = $user->id;
} else {
  $_SESSION["e_msg"] = $client->error;
}
header("Location: ".ROOT_DIR."homepage.php");
exit;
?>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require_once 'config.php';
class StructuredQuery{
    var $opt;
    var $pdo;
function __construct(){
    $opt = [
    PDO::ATTR_PERSISTENT         => FALSE,
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
    ];

    $this->pdo = new PDO(DB_DRIVER.":host=".DB_SERVER.";dbname=".DB_NAME, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, $opt);
}
// Cross Site Script  & Code Injection Sanitization
function xss_cleaner($input_str) {
    $return_str = str_replace( array('<',';','|','&','>',"'",'"',')','('), array('&lt;','&#58;','&#124;','&#38;','&gt;','&apos;','&#x22;','&#x29;','&#x28;'), $input_str );
    $return_str = str_ireplace( '%3Cscript', '', $return_str );
    return $return_str;
}
//SQLInjection detect
function sql_injection_detect($input_query){
    try{
        $blacklist=array('SELECT','WHERE','UPDATE','DELETE','INSERT','FROM','DROP','MERGE','SET','INSERT','REMOVE','REPLACE','QUERY');
        $err_level=0;
        foreach($blacklist as $blacklist_item){
            if(stripos($input_query,$blacklist_item)!==false){
                $err_level++; //Counter for number of blacklist words used. 2 means dangerous. Terminate immediately.
                if($err_level==2){
                    die('Was that an IT joke? Cause I am a 12th grader, not an IT Pro.');
                }
            }
        }
        return true;
    }catch(Exception $e){
            echo 'Exception Occured:',$e->getMessage(),"\n";
            die('You\'ve been Terminated');
        }
}
function getUserInfo($user_id){
    $user_id=xss_cleaner($user_id);
    if(sql_injection_detect($user_id)){
        $query=$pdo->prepare("select statement");
        $query->bindParam(":user_id",$user_id,PDO::PARAM_STR);
        $query->execute();
        $result=$query->fetch();
        $result["count"]=$query->rowCount();
        return $result;
    }
}
function putNewUserInfo($user_id,$name,$email){
    $user_id=$this->xss_cleaner($user_id);
    $name=xss_cleaner($name);
    $email=xss_cleaner($email);
    if(sql_injection_detect($user_id) && sql_injection_detect($name) && sql_injection_detect($email)){
        $query=$pdo->prepare("insert statement");
        $query->bindParam(":user_id",$user_id,PDO::PARAM_STR);
        $query->bindParam(":name",$name,PDO::PARAM_STR);
        $query->bindParam(":email",$email,PDO::PARAM_STR);
        $query->execute();
        return true;
    }else{
        return false;
        }
}
function modifyUserInfo($user_id,$name,$email,$clevel){
    $user_id=xss_cleaner($user_id);
    $name=xss_cleaner($name);
    $email=xss_cleaner($email);
    $clevel=xss_cleaner($clevel);
    if(sql_injection_detect($user_id) && sql_injection_detect($name) && sql_injection_detect($email) && sql_injection_detect($clevel)){
        $query=$pdo->prepare("update statement");
        $query->bindParam(":user_id",$user_id,PDO::PARAM_STR);
        $query->bindParam(":name",$name,PDO::PARAM_STR);
        $query->bindParam(":email",$email,PDO::PARAM_STR);
        $query->bindParam(":clevel",$clevel,PDO::PARAM_INT);
        $query->execute();
        return true;
    }else{
        return false;
        }
}
}

那么当前的行为是什么?!你有什么问题?您刚刚提供了2个文件…抱歉。忘记。LOL现在请看……我假设您的google登录正在通过您的
try/catch
运行,您在那里有一个输入错误
$sq.putNewUserInfo
,并强制重定向到
主页
,因为它看起来应该像
$sq->putNewUserInfo
。你能试试这些变化吗?此外,在调试时,最好将错误报告作为脚本中的第一件事打开:
ini\u set('display\u errors',1);错误报告(-1)。它将在将来帮助你!