/    Sign up×
Community /Pin to ProfileBookmark

Why $_GET No Match $_SESSION ?

Folks,

Check this:

If URL is:
http://localhost/test/home.php?user_id=15

Then, $_GET[‘user_id’] = 15;

Now, look at these codes:

1.

““
$_SESSION[‘user_id’] = 15;

if(!$_GET[‘user_id’] = $_SESSION[‘user_id’]);
{
echo “GET user_id:” . $_GET[‘user_id’]; echo “<br>”;
echo “SESSION user_id:” . $_SESSION[‘user_id’]; echo “<br>”;
die(“B.Invalid Request!”);
}
““

2.

““
$_SESSION[‘user_id’] = 15;

if(!$_GET[‘user_id’] == $_SESSION[‘user_id’]);
{
echo “GET user_id:” . $_GET[‘user_id’]; echo “<br>”;
echo “SESSION user_id:” . $_SESSION[‘user_id’]; echo “<br>”;
die(“B.Invalid Request!”);
}
““

3.

““
$_SESSION[‘user_id’] = 15;

if($_GET[‘user_id’] != $_SESSION[‘user_id’]);
{
echo “GET user_id:” . $_GET[‘user_id’]; echo “<br>”;
echo “SESSION user_id:” . $_SESSION[‘user_id’]; echo “<br>”;
die(“B.Invalid Request!”);
}
““

Both $_GET[‘user_id’] = 15
And
$_
SESSION[‘user_id’] = 15;

Right ?
So, why I get echoed: **B.Invalid Request!**

I get echoed:
**GET user_id:15
SESSION user_id:15
B.Invalid Request!**

I know no.2 is incorrect. But, still experimented.

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@xpacetwoOct 01.2020 — I am relatively new to PHP, so I realize this is very likely a beginners mistake; but I have done my due diligence and I have attempted to trouble-shoot the issue on my own, but with no luck.

First, I pass the values myusername and mypassword from the form to checklogin.php. From there it queries the database, and if a single row is returned where the username and password match, this code is run:

$_SESSION['myusername']=$myusername;

$_
SESSION['mypassword']=$mypassword;

header("location:login_success.php");

}

else {

echo "Wrong Username or Password";

}

?>

Now, I understand writing your own login mechanism is frowned upon --especially since mine doesn't even work. I understand this; but at this point, getting it to work is more of a learning experience for myself than a practical application.

From here I am directed to this page (‍login_success.php‍), which should only load if ‍$_SESSION['myusername']‍ is set or rather, I am "logged in". [https://jiofilocalhtml.run](https://jiofilocalhtml.run) [https://forpc.onl](https://forpc.onl)
Copy linkTweet thisAlerts:
@developer_webauthorOct 04.2020 — @xpacetwo#1623836

My login:

error_reporting.php

<?php

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
ini_set('error_reporting',E_ALL);//error_reporting(E_ALL);
?>



conn.php

<?php

$server = 'localhost';
$db_user = 'root';
$db_password = '';
$db = 'test';

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect($server,$db_user,$db_password,$db);
mysqli_set_charset($conn,'utf8mb4');

if(mysqli_connect_error())
{
echo "Mysqli Connection Error" . mysqli_connect_error();
}
elseif(mysqli_connect_errno())
{
echo "Mysqli Connection Error Number" . mysqli_connect_errno();
}

?>


login.php

<!DOCTYPE HTML>

<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>

<form method="GET" name="login_form" id="login_form" action=<?php echo $_SERVER['PHP_SELF'];?>>
<legend>
<label for="domain_email">Domain Email *</label>
<input type="domain_email" name="domain_email" id="domain_email" placeholder="Type here your Email Address belonging to your Page's Domain ..." REQUIRED>
</legend>
<br>
<legend>
<label for="password">Password *</label>
<input type="password" name="password" id="password" REQUIRED>
</legend>
<br>
<button name="login_button" id="login_button" value=" ">Login</button>
<br>
<input type="reset">
</form>

</body>

</html>

<?php

require 'conn.php';
require 'error_reporting.php';

session_start();

$domain_email = $password = "";

if($_SERVER['REQUEST_METHOD'] == "GET")
{
$domain_email = $_GET['domain_email'];
$password = $_GET['password'];

//STEP 1
if(!filter_var($domain_email,FILTER_SANITIZE_EMAIL))
{
die("Input a VALID Email Address!");
}
if(!filter_var($domain_email,FILTER_VALIDATE_EMAIL))
{
die("Input a VALID Email Address!");
}

if(!filter_var($password,FILTER_SANITIZE_STRING))
{
die("Input a VALID Password!");
}

//STEP 2

function validate_input($data_input)
{
$data_input = trim($data_input);
$data_input = stripslashes($data_input);
$data_input = strip_tags($data_input);//I ADDED THIS LINE. IS IT NECESSARY OR IS THE FILLOWING ENOUGH ? : $data_input = stripslashes($data_input);

return $data_input;
}

$domain_email = validate_input($domain_email);
$password = validate_input($password); echo "Password" . $password; echo "<br>";

echo "Password" . $password; echo "<br>";

$query_1 = "SELECT Count(id) FROM users WHERE domain_email = ? AND password = ?";

$stmt_1 = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt_1,$query_1))
{
die("A.Query Failed!");
}
else
{
mysqli_stmt_bind_param($stmt_1,'ss',$domain_email,$password);
mysqli_stmt_execute($stmt_1);
$result_1 = mysqli_stmt_bind_result($stmt_1,$row_count);
mysqli_stmt_fetch($stmt_1);
echo "Row Count:" . $row_count; echo "<br>";

if(!$result_1)
{
die("B.Query Failed!");
}

mysqli_stmt_close($stmt_1);

if($row_count<1)
{
die("Login failed!");
}
else
{
$_SESSION['domain_email'] = $domain_email;
$_SESSION['password'] = $password;

$query_2 = "SELECT id FROM users WHERE domain_email = ? AND password = ?";
$stmt_2 = mysqli_stmt_init($conn);

if(!mysqli_stmt_prepare($stmt_2,$query_2))
{
die("C.Query Failed!");
}
else
{
mysqli_stmt_bind_param($stmt_2,'ss',$domain_email,$password);
mysqli_stmt_execute($stmt_2);
$result_2 = mysqli_stmt_bind_result($stmt_2,$user_id);
mysqli_stmt_fetch($stmt_2);

if(!$result_2)
{
die("D.Query Failed!");
}

mysqli_stmt_close($stmt_2);

$_SESSION['user_id'] = $user_id;
header("location:home.php?user_id=$user_id");
}
}
}
}

?>


Learn from that.
Copy linkTweet thisAlerts:
@developer_webauthorOct 04.2020 — Php Folks,

What's wrong with my codes in my original post ?
Copy linkTweet thisAlerts:
@NachfolgerOct 04.2020 — @developer_web#1623882

Incorrect. You're assigning $_SESSION['user_id'] to $_GET['user_id'], and you're also negating the value of $_GET['user_id'].
``PHP<i>
</i>if(!$_GET['user_id'] = $_SESSION['user_id']);
{<i>
</i>
`</CODE>

Also incorrect, you're NEGATING the value of <C>
$_GET['user_id']</C> and I have no idea why
<CODE lang="PHP">
`PHP<i>
</i>if(!$_GET['user_id'] == $_SESSION['user_id']);<i>
</i>
`</CODE>

<STRONG>**Correct evaluation, INCORRECT PHP if-statement design**</STRONG>. I have NO idea why you keep adding a colon to the end of if-statements.
<CODE lang="PHP">
`PHP<i>
</i>if($_GET['user_id'] != $_SESSION['user_id']);
{<i>
</i>
``
Copy linkTweet thisAlerts:
@developer_webauthorOct 07.2020 — @Nachfolger#1623886

Thanks.

That semi colon is typo.

What you mean "negating $_GET value ? You mean with these 2:


if(!$_GET['user_id'] = $_SESSION['user_id']);
{



if(!$_GET['user_id'] == $_SESSION['user_id']);
{


I am actually checking whether $_GET has value or not when intending to check whether $_GET matches with $_SESSION value or not ?

And to check if $_GET value matches with $_SESSION value or not, I must do it like this:


if($_GET['user_id'] != $_SESSION['user_id']);
{


Yes ?
Copy linkTweet thisAlerts:
@NachfolgerOct 07.2020 — @developer_web#1624025

Somewhat. A exclamation mark before a variable (in this case) "negates" it's value.

``PHP<i>
</i>(!0 == 1)<i>
</i>
`</CODE>
Is true. Why? Because <C>
!0</C> is <C>1`.

Explore this more on your own.
Copy linkTweet thisAlerts:
@developer_webauthorOct 12.2020 — @Nachfolger#1624034

Thanks.

Sticking to this then:

if($_GET['user_id'] != $_SESSION['user_id']);
{


I wonder if I will remember all this negating stuff. One day, might open another thread asking same/similar question on it due to forgetting your answer here.
Copy linkTweet thisAlerts:
@developer_webauthorOct 12.2020 — @Nachfolger#1624034

Does this mean following wrong too ?

A.

if(!ISSET($_GET['col']))
{
die("Type your Column!");
}
$col = $_GET['col'];


Shall I go for these FOLLOWING instead ?

B.

if(ISSET($_GET['col']))
{
$col = $_GET['col'];
}
else
{
die("Type your Column!");
}


Or, even this:

C.

if(ISSET($_GET['col'])===TRUE) //This should not be "==". Right ?
{
$col = $_GET['col'];
}
else
{
die("Type your Column!");
}


D.

if(ISSET($_GET['col'])===FALSE) //This should not be "==". Right ?
{
$col = $_GET['col'];
}
else
{
die("Type your Column!");
}


E.

if(ISSET($_GET['col'])!=TRUE)
{
die("Type your Column!");
}
else
{
$col = $_GET['col'];
}


If so, then I guess I learnt something from you.
Copy linkTweet thisAlerts:
@developer_webauthorDec 03.2020 — @NogDog,

Care to answer my previous post ?

Thanks!
Copy linkTweet thisAlerts:
@sadatvidDec 03.2021 — i've tried the codes but it didn't worked for me.[.](https://get-vidmate.com) [.](https://instasave.onl/)
Copy linkTweet thisAlerts:
@mastertech2021Dec 21.2021 — Wonderful information admin thanks for this post

https://www.alltechdownload.com/

https://www.routerlogin-passwordip.com/
Copy linkTweet thisAlerts:
@techsolverMay 04.2022 — Thanks for sharing this information. https://www.techsolveguide.com/

https://www.playstoreinfo.com/
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...