I've got a problem with a SESSION, inside a function.
How can I set $_SESSION['idUser'] = $result['idUser']; to a SESSION, which I can use for upcoming activity's.
<?php
// Session starts
session_start();
class DB_Functions {
public function getUserByEmailAndPassword($email, $password) {
$result = mysql_query("SELECT * FROM User WHERE emailadress = '" . $email . "'") or die(mysql_error());
// check for result
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
$result = mysql_fetch_array($result);
//This is the session I want to use
$_SESSION['idUser'] = $result['idUser'];
return $result;
Assuming this is just a snippet of a larger class, I see no reason why this line:
$_SESSION['idUser'] = $result['idUser'];
wouldn't work (again assuming that $result['idUser'] contains a value). Ensure that you have session_start();
called on all pages that you want to use the global session variable.
Delete the space before <?php
. Before session_start()
there shouldn't be any output.
I suggest using the following in the first place:
I highly recommend it.
Example code:
use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
$session->start();
// set and get session attributes
$session->set('name', $result['id_user']);
$session->get('name'); //Easy to get 'id_user' in another page.
//Other examples:
// set flash messages (appears only once, for warnings/alerts)
$session->getFlashBag()->add('notice', 'Profile updated');
// retrieve messages
foreach ($session->getFlashBag()->get('notice', array()) as $message)
{
echo '<div class="flash-notice">'.$message.'</div>';
}
If you like like what you see here are the links:
Make sure your PHP session isn't already started before using the Session class. If you have a legacy session system that starts your session, see Legacy Sessions .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.