Jump to content

session_start


cfobare

Recommended Posts

I would like to carry the username not the password to the following pages. I have 

 

<?php
session_start();   // Store Session Data   
$_SESSION['login_user'] = $username; // Initializing Session with value of PHP Variable   
echo $_SESSION['login_user'];   
?>
at the top of the page and

 

<div id="profile">   
<b id="welcome">Welcome : <i><?php echo $login_user; ?></i></b>   
<b id="logout"><a href="logout.php">Log Out</a></b>
</div>
 

in the body of the HTML

Link to comment
https://forums.phpfreaks.com/topic/295948-session_start/#findComment-1510308
Share on other sites

<?php
session_start();   // Store Session Data   
$_SESSION['login_user'] = $username; // Initializing Session with value of PHP Variable   
echo $_SESSION['login_user'];   
?>

session_start doesn't store session data it starts the session, the $_SESSION function stores session data.

By the looks of it you are trying to store the username on login and echo it into the index page?

Try this:

 

login

<?php

session_start(); //Start session

//This is only an example you store the session data when you are logging in

if($count->rowCount > 0)
{

//Store session data

$_SESSION['username'] = $username;

}

?>

index

<?php

session_start();

if(isset($_SESSION['username']))
{

} else {

echo "Couldn't set session!";

}

?>

Then you can use

<?php echo $username; ?>
Link to comment
https://forums.phpfreaks.com/topic/295948-session_start/#findComment-1510387
Share on other sites

since we have taken a pedantic turn here: $_SESSION isn't a function, it's a superglobal variable array.  also, you should check for if(!isset()) rather than if(isset()) as the latter can still throw a "Warning: <xxx> is undefined" in the event that the thing you are checking isn't actually set (at least in some older versions of PHP, dont know about the more recent versions because I stopped doing it...).

Link to comment
https://forums.phpfreaks.com/topic/295948-session_start/#findComment-1510390
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.