Hi
I would like to store a userID into $_SESSION then reference the $_SESSION in another page. I get how the Session function supposed to work but need help with the syntax please?
I have a table called users which stores uid, username and password
I have then started the session function like this
<?php
class DB_Functions {
session_start();
Then tried to use the $_SESSION here:
/**
* Get user by email and password
*/
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$_SESSION[email] =$email
$result = mysql_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password equality
if ($encrypted_password == $hash) {
// user authentication details are correct
return $result;
}
} else {
// user not found
return false;
Then called it in the next page as below:
<?php
session_start();
$result = mysql_query("SELECT users.uid, users.name
FROM users
WHERE EXISTS (SELECT 0 FROM available A1, available A2
WHERE users.uid <> $_SESSION[email])") or die(mysql_error());
But got syntax error
How do i store the ID from the user returned in the 1st query then pass to the second query correctly?
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
Second, the PHP session is really easy to use correctly, and many people over-think it. This article tells what you want to do.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html
Looking at this code snippet, it appears that it would fail, since you cannot have function calls outside of class methods. Session_start() does not belong here.
Open in new window
You may have had some of the issues because of the use of quotation marks in PHP. They are required in some instances, and using them correctly is an important part of the language. I'm not sure about that, because PHP has many installation options that can impinge on the use of quotes, but you might want to check the code over to make sure it's coded correctly to do what you want.https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
If you can show us where you got the syntax error and what the error message said, we can probably help with that.