Reputation: 133
I have a form that inserts some things to MySQL database.
Username. Password. ID.
I store the username into a session, and then I change headers (.php?recovery=success). After that, I want to test if my session worked (For later use, so then I can go to that username's column in the database to fetch the ID we inserted, if I will just do echo $ID, it will generate a new ID.
else if (isset($_GET['recovery']) && $_GET['recovery'] == 'success')
{
echo $_SESSION['user'];
/*
***REMOVED FOR TESTING***
$fetch = $connect->query("SELECT * FROM users WHERE username = ':username' LIMIT 1");
$fetch->bindValue(':username', $_SESSION['user']);
$fetch->execute();
while($row = $fetch->fetch( PDO::FETCH_ASSOC )) {
echo $row['recover_id'];
}
*/
}
Problem:
I filled out the form, on submit it took me to recover.php?recovery=success and then when echoing the session data, there is no output?
I'm requiring the file session.inc.php which opens a new session.
This is the whole code:
What am I doing wrong?
I am new to PHP, and specially PDO. Thanks!
ADDITIONAL:
Session.inc.php
<?php
session_start();
?>
Upvotes: 0
Views: 203
Reputation: 614
It looks like your code is storing information into the Session and then redirecting with a call to header (see snippet).
// Let's store these into a session now.
$_SESSION['user'] = $username;
$_SESSION['pass'] = $password;
//Now let's refresh the page, to a different header.
header('Location: recover.php?recovery=success');
Just to be cautious; I would advise an explicit call to
Before your redirect, to ensure that the session data is being properly saved before execution leaves the current page...like so:
// Let's store these into a session now.
$_SESSION['user'] = $username;
$_SESSION['pass'] = $password;
session_write_close();
//Now let's refresh the page, to a different header.
header('Location: recover.php?recovery=success');
Upvotes: 2
Reputation: 133
Problem was:
I put the code inside the statement that checking if recovery is NOT set
if (!isset($_GET['recovery'])) {
at the top of the code, if you didn't see in the pastebin.
I put the code outside of that statement, it it worked.
Upvotes: 1