Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Getting username to display if logged in, or display login link if logged out.

php script

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 strages87

strages87

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 02 March 2015 - 06:56 PM

Hello, I am in the process of learning PHP and was asked to code a MySQL / PHP website for a friend of mine. I got the MySQL and PHP portions pertaining to login / logout working with no problem, and am now having an issue with this current task.

 

At the top left corner of the site, I want it to show:

 

Username (Log Out)

 

if the user is logged in, or

 

(Login / Register)

 

if the user is not logged in, according to whether $_SESSION['logged_in'] is set to true or not.

 

This is the code I have written so far on the test page:

<?php
if(!isset($_SESSION['username']) || empty($_SESSION['username'])) {
	echo '(<a href="login.php">Login/Register</a>)';
} else {
	echo $_SESSION['username'] . '(<a href="includes/logout.inc.php">Log Out</a>)';
}
?>

I have also tried:

<?php
if($_SESSION['logged_in']) {
	echo $_SESSION['username'] . '(<a href="includes/logout.inc.php">Log Out</a>)';
} else {	
        echo '(<a href="login.php">Login/Register</a>)';
}
?>

So far, all it will do is show the login/register part, but will not show the username (logout) part at all.  Even when logged in, it still shows login/register.

 

At the top of the page, I have

<?php
session_start();

require_once('includes/functions.inc.php');

if (check_login_status() == false) {
	redirect('login.php');
}
?>

which does what it is supposed to.  Any help on what I might be doing wrong would be appreciated, as every site I've tried that touches this topic says to do something similar or the same as what I am doing already.

 

Thanks in advance, and if you need more details, feel free to ask.



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 02 March 2015 - 07:56 PM

Welcome aboard,

 

This is the code you are looking for (if no typos):

<?php
 
$toEcho = '(<a href="login.php">Login/Register</a>)';

if(isset($_SESSION['username']) && !empty($_SESSION['username']))
{
   if($_SESSION['logged_in'])
   {
    $toEcho = $_SESSION['username'] . '(<a href="includes/logout.inc.php">Log Out</a>)';
   }    
} 

echo $toEcho;
?>

Edited by BlackRabbit, 02 March 2015 - 07:58 PM.


#3 strages87

strages87

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 02 March 2015 - 09:10 PM

Just tried that, and it is still doing the same thing.

 

If you would like to see the problem in action, I can provide a link to the website.  It's a framework only at the moment until I get this problem fixed.

 

Also, just to be safe, here's the entire code for index.php (the page I am currently testing this script on):

<?php
session_start();

require_once('includes/functions.inc.php');

if (check_login_status() == false) {
	redirect('login.php');
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Federation Trade Co.</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
</head>

<body>
<div id="wrapper">
<div id="header">
<?php
 
$toEcho = '(<a href="login.php">Login/Register</a>)';

if(isset($_SESSION['username']) && !empty($_SESSION['username']))
{
   if($_SESSION['logged_in'])
   {
    $toEcho = $_SESSION['username'] . '(<a href="includes/logout.inc.php">Log Out</a>)';
   }    
} 

echo $toEcho;
?>
</div>
<div id="banner"><img src="" alt="" width="800px" height="150" /></div>
<div id="links"></div>
<div id="content">

<h1>Federation Trade Co. Main Page</h1>
<p>You are currently logged in.  You may log out using the button below.</p>
<p><a href="includes/logout.inc.php">Log Out</a></p>

</div>
<div id="sidebar"></div>
<div id="footer"></div>
<body>
</body>
</html>


#4 strages87

strages87

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 03 March 2015 - 07:42 AM

Just found it out after spending another 4 hours scouring the web. I added this to my login.inc.php file:

if (is_object($result) && $result->num_rows == 1) {
		$_SESSION['logged_in'] = true;
		redirect('../index.php?user='.$username); //added ?user='.$username to end of redirect
	} else {
		redirect('../login.php');
	}
}

Then, on index.php, I changed / added the following:

<?php
$user = $_GET['user']; //added this line
session_start();

require_once('includes/functions.inc.php');

if (check_login_status() == false) {
	redirect('login.php');
}
?>

and

<?php
if(!empty($_GET['user'])) {
	echo $user . ' (<a href="includes/logout.inc.php">Log Out</a>)';
} else {
	echo '(<a href="login.php">Login/Register</a>)';
}
?>
// replaced original if statement with the above code.

Thanks for the help, and if anyone else has this problem, feel free to pass this along :)






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download