-1

我用 Facebook 登录 API 创建了一个登录页面。当用户登录时,我已将用户数据(姓名、性别等)存储到 MySQL 数据库中(表中的“gorg”列除外)。

然后,我会将用户重定向到“newgg.php”,它有两个链接“Giver”和“Gatherer”。因此,用户可以选择其中之一。

我的示例代码:

<?php

session_start();
error_reporting(E_ALL);

include('src/sql_handler.php');
include('src/facebook_handler_core.php');

$new_fb = new facebook_handler_core;
$new_fb->run();

if (isset($_SESSION['gorg']) == "Gatherer") {
  header('Location: map.php');
}

?>

我的目标是根据他们第一次访问页面时按下的按钮重定向他们,这是按钮代码

<form method="post" action="<?php echo $PHP_SELF;?>">
      <input type="submit" class="button orange" name="Giver" value="Giver">
</form>

<form method="post" action="<?php echo $PHP_SELF;?>">
      <input type="submit" class="button orange" name="Gatherer" value="Gatherer">
</form>

现在最后但并非最不重要的一点是,如果他们之前已经选择了他们的用户类型,则只需根据用户表中“gorg”列读取的内容来重定向他们。

关于为什么我的代码无法正常工作的任何想法?

以防万一您需要它们,这里是 sql_handlers

<?php

class MySQL_Con {
private $host = 'localhost',
        $user = 'NUNURBSINESS',
        $pass = 'ASKMEANDMAYBE',
        $db = 'teknolog_fruitforest',
        $_CON;

function MySQL_Con() {

    $this->_CON = mysql_connect($this->host, $this->user, $this->pass);
    if(!$this->_CON)
        die(mysql_error());
    else {
        $select_db = mysql_select_db($this->db);

        if(!$select_db)
            die('Error Connecting To Database'.mysql_error());
    }
}

function End_Con() {
    mysql_close($this->_CON);
}
}
?>

现在是 facebook_handler_core.php

<?php

class facebook_handler_core extends MySQL_Con {
public $session,$_INFO = array(),$U_INFO = array();

public function run() {
    require('src/facebook.php');

    $set_fb = new Facebook(array(
                'appId' => 'MYAPPID',
                'secret' => 'CANTTELLYOU',
                'cookie' => true));

    $this->session = $set_fb->getUser();

    if($this->session != 0) {
        $this->_INFO = $set_fb->api('/me');

        if(!empty($this->_INFO))
            $this->fb_session_handler();
    }
}

function fb_session_handler() {
    $SQL_CON = new MySQL_Con;
    $SQL_CON->MySQL_Con();
    $query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND email = '" .mysql_real_escape_string($this->_INFO['email'])."'") or die(mysql_error());

    if(mysql_num_rows($query) > 0) {
        $this->U_INFO = mysql_fetch_array($query) or die(mysql_error());
    } else {
        $photolink = 'http://graph.facebook.com/'.$this->session.'/picture?type=square';

        $query = mysql_query("INSERT INTO users(oauth_uid, oauth_provider, username, first_name, last_name, email, pic_square, gorg, gender)VALUES('".mysql_real_escape_string($this->session)."','facebook',    '".mysql_real_escape_string($this->_INFO['name'])."',      '".mysql_real_escape_string($this->_INFO['first_name'])."','".mysql_real_escape_string($this->_INFO['last_name'])."','".mysql_real_escape_string($this->_INFO['email'])."','".mysql_real_escape_string($photolink)."','null','".mysql_real_escape_string($this->_INFO['gender'])."')") or die(mysql_error());

        $query = mysql_query("SELECT * FROM users WHERE email='".mysql_real_escape_string($this->_INFO['email'])."'") or die(mysql_error());

        $this->U_INFO = mysql_fetch_array($query) or die(mysql_error());

    }

    $SQL_CON->End_Con();
    $gorg = $this->U_INFO['gorg'];

    if($gorg != null) {
        $_SESSION['gorg'] = $gorg;
    }

    $_SESSION['email'] = $this->U_INFO['email'];
    $_SESSION['image'] = $this->U_INFO['pic_square'];
    $_SESSION['gender'] = $this->U_INFO['gender'];

    if($gorg != null) {
        if($gorg == 'Giver') {
            //redirect to Giver
            header('Location: picktreetype.php');
        }

        if($gorg == "Gatherer") {
            //redirect to Gatherer
        }
    }

    return true;
}

function update_user($param) {
    $SQL_CON = new MySQL_Con;
    $SQL_CON->MySQL_Con();

    if($param == 'Giver')
        $query = mysql_query("UPDATE users SET gorg='".mysql_real_escape_string($param)."', FF_Points='100' WHERE email='".mysql_real_escape_string($_SESSION['email'])."'") or die(mysql_error());

    if($param == 'Gatherer')
        $query = mysql_query("UPDATE users SET gorg='".mysql_real_escape_string($param)."', FF_Points='30' WHERE email='".mysql_real_escape_string($_SESSION['email'])."'") or die(mysql_error());

    $SQL_CON->End_Con();

    if(!$query)
        return false;
    else
        return true;
}
}
?>

在此先感谢,在获得帮助和适当的指导方面,我无法从该网站获得足够的帮助,我非常感谢过去任何人给予我的所有帮助。

4

1 回答 1

0

问题是你在做

isset($_SESSION['gorg']) == "Gatherer"

asisset()返回一个布尔结果,使用==将匹配任何非显式假值。===如果您使用(身份比较运算符),您将获得问题的直接证据。

因此,在您的情况下,“Gatherer”被评估为非 FALSE,即 TRUE。

每次。

你不应该使用这种比较;而是尝试:

isset($_SESSION['gorg']) && $_SESSION['gorg'] == "Gatherer"

gorg如果您希望在进行任何其他评估之前继续检查是否已设置。

于 2012-08-02T06:48:53.567 回答