Jump to content

display welcome message after login


cdm30

Recommended Posts

Hi there,

New to the forum... thank you in advance for any help!

 

I created a login system using a tutorial found online. Everything works perfectly.. but now the client wants "Hello [First Name]" displayed after logging in. I've tried about 12 different tutorials at this point and can't seem to tweak them enough to work with my code.

 

A lot of the tutorials have session_start(); at the top of their protected (welcome.php) page. But mine seems to be held in a variable in another script and the top of my welcome.php looks like this:

 

<?PHP

require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}

?>

 

This is what I have on my welcome page as well but I don't know how to make this all come together and work..

 

Hello <?php echo $_SESSION['name']; ?>

 

I'm using mySQL 5.0, and php 5.2. Will anyone shed light on this for me? I'll provide any other info you may need..

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/234874-display-welcome-message-after-login/
Share on other sites

<?PHP

require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
if (isset($_SESSION['name'])){
$name = $_SESSION['name'];
echo "Hello $name";
?>

 

using the isset() function to check that the session variable of ['name'] exists (which it only will while someone has logged in) lets you display the name on condition that it is a logged in member that is viewing the page.

If the page isn't embeded in another one that already has it, you need to include

 

session_start();

 

before any of the page code.  This tells the server you want to access the session information that you have stored elsewhere.  I made a login thing for a section of my site that does what you're looking to do if you want to take a look at it.

Thanks guys for the info..

 

Muddy_Funster I used your code and added session start to top like others mentioned, I've also kept Hello < /? php echo $_SESSION['name'];  ? /> in the body where I want it to display. But it's still not working...

 

here's the top of the page now:

 

<?PHP
session_start();

require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
    $fgmembersite->RedirectToURL("login.php");
    exit;
}
if (isset($_SESSION['name'])){
$name = $_SESSION['name'];
echo "Hello $name";
}
?>

 

membersite_config.php is where the database connection info is stored. And all the session info regarding input and login username password etc is in a page called fg_membersite.php. Might be a dumb question but shouldn't this page be in my welcome.php script some where? Since this is teh page with session variables? I don't know though since everything else works fine..

 

CK9 I'd love to see what you've done on your own site, thank you!

 

Here's what I did while working on a project with a few other people:

 

//this section is a bit unrelated, but I left it in so I can explain where the session_start() is at
<?
$sp = $_GET['pg'];
function pgswtch($sp)
  {
   include("header.html");
   echo "<table cols='2' border='0' cellpadding='10' cellspacing='0' align='center' width='100%'>";
   echo "<tr>";
   echo "<td width='16%'>";
   include('navbar.html');
   echo "</td><td>";
   switch($sp)
    {
     case 'info':
      include('info.html');
      break;
     case 'maps':
      include('maplist.html');
      break;
     case 'images':
      include('piclist.html');
      break;
     case 'other':
      include('misc.html');
      break;
    }
   echo "</td></tr></table>";
   include('footer.html');
   }

//and this is where it seems the most relevant to what you're doing.
if ($_GET['u'] == '' || empty($_SESSION['user']))
  {
   echo "You have not been given authorization to view this folder!";
  }
else
  {
   echo "<center><font color='00cc00' size='7'>Welcome back" . $_SESSION['user'] . "!</font></center>";
   pgswtch($sp);
  }
?>

 

Because I use a switch to generate a page layout similar to HTML frames without all the annoyances of frames, I was able to store the session_start() in the header.html file.  It works perfectly this way across all pages accessed through the index.php page.  The if statement is just a safegaurd that was in place to preven people from messing with the project.  You could use it similarly to make sure the user session variable is actually recorded.  In example:

 

if(empty($_SESSION['user']))
{
  echo "No username has been indicated.";
}

 

If you do this and then see that message on the screen, then check the login verification that stores the data in the variable for a session_start(), as this needs to be on every page that uses session variables.

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.