PHP comparison operator == not working as required. Strange behavior

98 views Asked by At

I'm trying to update my user's account once he/she has successfully authenticated via Twitter(Twitter Oauth). The callback url is www.xyxy.com/email.php

I want the user to updated his profile and change email id once he/she has been authenticated. My `oauth table consists of the following columns: twitter handle, access_token, access_secret_key, email

The email column is inserted with a garbage value in my INSERT command since I will be updating that column in my email.php page. I want the user to see email.php page only if he hasn't updated his email id, else he should be landing on the index.php page.

To achieve that, I've used the SELECT command and stored the email field in a variable. Then I've compared this variable with the garbage value and accordingly the header() function has been written. But the control never seems to enter the if condition.

email.php

<?php
/**
 * @file
 * User has successfully authenticated with Twitter. Access tokens saved to session and DB.
 */

/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');
require('mysqlconnection.php');

/* If access tokens are not available redirect to connect page. */
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
    header('Location: ./clearsessions.php');
}
/* Get user access tokens out of the session. */
$access_token = $_SESSION['access_token'];


/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);



/* If method is set change API call made. Test is called by default. */
$data = $connection->get('account/verify_credentials');


$screen_name = $data->screen_name;


/*Updating oauth table with user tokens.*/
try {


    // begin the transaction
    $conn->beginTransaction();
    // our SQL statememtns


    $conn->exec("INSERT IGNORE INTO oauth (twitterhandle,access_token, oauth_token_secret, email) 
    VALUES ('".$screen_name."','".$access_token['oauth_token']."','".$access_token['oauth_token_secret']."','[email protected]')");


    // commit the transaction
    $conn->commit();
   // echo "New records created successfully";
    }
catch(PDOException $e)
    {
    // roll back the transaction if something failed
    $conn->rollback();
    echo "Error: " . $e->getMessage();
    }

$stmt = $conn->prepare("SELECT email FROM oauth WHERE twitterhandle = :value");
$stmt->execute(array(':value' => $screen_name));

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total = count($result);
//print_r($result);
$email = $result;
print_r($email);


//echo gettype($email[0][0]);
//echo $email[0][0];

$fetchedresult = $email[0]['email'];
$garbagevalue = "[email protected]";

//echo $fetchedresult;

Update 1: Output of echo $fetchedresult;

[email protected]

Output of print_r($email);

Array ( [0] => Array ( [email] => [email protected] ) )

 //echo $garbagevalue;

if($fetchedresult == $garbagevalue)
{
echo"if part";

 //take email id from user via a form.
/*
$sql1 = UPDATE oauth   
   SET `emailid` = :newemailid
 WHERE `twitterhandle` = :screen_name
 */
 header('Location: http://www.xyxyxy.com/email.php'); 
}

if($fetchedresult != $garbagevalue)
{
echo "Else part executed";
//header('Location: http://www.xyxyxy.com/index.php'); 
}

$conn = null;

?>
<!DOCTYPE html>
<html>
<head>
    <title>XYXY - Index</title>
    <link rel="stylesheet" href="css/style.css">
</head>


<body>

<center>
    <div id="content">

     <h3>Account Update</h3>
     <form id="changemail" action="" method="post">
        <p>
          <label for="emailid">Update your account Email ID:</label>
          <input type="text" name="emailid" id="emailid"/>
        </p>


        <p>
          <input type="submit" name="btnUpdate" value="Update" />
        </p>
      </form>

      <td>&nbsp;</td>
<p>
 <?php
    echo "<p align='left'><a href='clearsessions.php' >Sign Out</a></p>";
    ?>
  </p>

        </div>
</center>
</body>
</html>

Why am I welcomed with this behavior and how do I achieve the desired scenario?

0

There are 0 answers