Avatar of tonelm54
tonelm54
 asked on

== not working the way Id expect it

Im trying to test if a variable is the same as another vairable:-
if ($_SESSION['loggedIn'] == $authKey) {

Open in new window


Which its saying it is, however it shouldn't, to debug I added:-
		echo "authKey = " . $authKey . "<br/>";
		echo "SESSION loggedIn - " . $_SESSION['loggedIn'] . "<br/>";

Open in new window


Which returns:-

authKey = 5f8e4f8b-f7db-4c6c-8beb-0336a7e1b443
SESSION loggedIn - 1

And then to test further I used:-
		if ($_SESSION['loggedIn'] == $authKey) {
			echo "T";
		} else {
			echo "F";
		}

Open in new window


Which returns T.

I dont understand why its returning that it is the same, when it isnt.

Any ideas how to fix it and why its happening?
PHP

Avatar of undefined
Last Comment
tonelm54

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Marco Gasi

Because, when you compares a number with a string, the string is converted in a number; any string which doesn't start with a number is converted to 0, the other to 1, so your comparison is

if 1 == 1 -> true

Replace equal operator == with identical operator === to avoid this issue.

But your code doesn't make sense to me because you're comparing a boolen value (loggedin which can be 0 or 1) with a string which represents a token so you'll always get false. You should change the logic and store ina  cookie or in the $_SESSION array the authKey when it is assigned and then perform your comparison against the coockie or the $_SESSION value
Ray Paseur

@Marco: I don't see it that way.  Check out this example.  That's why I think we are missing an important piece of the puzzle.

Please see: https://iconoun.com/demo/temp_tonelm54_FALSE.php
<?php // demo/temp_tonelm54_FALSE.php
/**
 * https://www.experts-exchange.com/questions/28958236/not-working-the-way-Id-expect-it.html
 *
 * An Example SSCCE
 * http://sscce.org/
 *
 * Note that there is no session_start() statement in this example.  Not needed for this test.
 *
 */
error_reporting(E_ALL);
echo '<pre>';


// ASSIGN OUR TEST VARIABLES
$authKey = "5f8e4f8b-f7db-4c6c-8beb-0336a7e1b443";
$_SESSION['loggedIn'] = "1";

// SHOW OUR TEST VARIABLES
echo "authKey = " . $authKey . "<br/>";
echo "SESSION loggedIn - " . $_SESSION['loggedIn'] . "<br/>";

// USE OUR TEST VARIABLES
if ($_SESSION['loggedIn'] == $authKey) {
	echo "T";
} else {
	echo "F";
}

Open in new window

Outputs:
authKey = 5f8e4f8b-f7db-4c6c-8beb-0336a7e1b443
SESSION loggedIn - 1
F

Open in new window

That aside, the correct (working) design for PHP client authentication is shown in this article:
https://www.experts-exchange.com/articles/2391/PHP-Client-Registration-Login-Logout-and-Easy-Access-Control.html
Marco Gasi

Wooops, I see...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Dave Baldwin

One of the thing that "session_start()" does is create or find the file that session data is stored in.  While it may not affect this simple demo, it is never a good idea to try to use $_SESSION variables without it.  You certainly can't expect to store any $_SESSION variables without it and expect them to be there on a second page.
Ray Paseur

Dave: Agreed, 100%, and that's why I asked about it right at the top.

In any real-world example session_start() would be a common-sense thing to do, but it's not necessary in this instance, and I didn't include it to avoid cluttering up my session with E-E test data.  I did, however, make note of this in the script comments, since it's one of those things that requires explanation.
tonelm54

ASKER
Weirdly, my code still doesn't produce the right answer, but when I run yours it does, so must be something weird happening somewhere else thats throwing it off. Its only POC anyways
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.