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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.