PHP: Putting Variable into Hyperlink Text Problem

jeeter

New Member
Messages
9
Reaction score
0
Points
0
Hi,

I am having a problem with getting some PHP code to work...

This is what I'm trying to do:

I am attempting to create my own simple login script, and so far it works properly for comparing Username and Password values to the DB and returning whether the user is correctly logging in or not(correct username & password).

In my script for checking the username & password to the DB (checklogin.php), I am trying to set up Session variables(loggedin and userid):

PHP:
//Set loggedin to 1 meaning user is CURRENTLY LOGGED IN.
$_SESSION['loggedin'] = 1;
//Set session userid to the username used. Stored in $myusername
$_SESSION['userid'] = $myusername;

//Redirect to index, now logged in
header(location:index.php);
$myusername is a username variable that was stored from the $_POST['username'] when the script was called. Note that this $myusername variable worked for when I checked in the database, so it should store that variables value into $_SESSION['userid'] yes?

The problem at hand here is that I want my header(header.php) to check if the user is logged in and IF SO then change the link 'Login' to a link titled with their username.

*Example:*
When NOT logged in, this is the menu:
<a href="">Home</a> | <a href="">About</a> | <a href="">Login</a>

When they ARE logged in, this is the menu(using username Jeeter):
<a href="">Home</a> | <a href="">About</a> | <a href="">Jeeter</a>
*End of Example*

This is the code in my header (header.php) file that checks if the user is logged in, and if they are, attempts to build a hyperlink using their name:

PHP:
 //See if user is already logged in
  if ($_SESSION['loggedin'] = 1) //Logged in
  {
   //Store sessions userid into $name variable
   $name = $_SESSION['userid']; 

   //TODO: have username show in hyperlink..
   //Create hyperlink using $name variable
   echo "<td><a href=\"user.php\">{$name} Panel</a></td>";
  }
  else //Not Logged in
  {
   echo "<td><a href='login.php'>Login</a></td>";
  }
When I attempt to use this scripting and all the files together I get a successful login check, and a successful redirect. And it IS able to check the $_SESSION['loggedin'] to get 0 or 1 to determine the user being logged in or not...but when it goes to create the hyperlink I get this:

<a href="user.php">Panel</a>

...But it should be:

<a href="user.php">Jeeter Panel</a>


Any help is very appreciated!! If you need anymore information to determine what is is that I need, just drop a reply.

Thank you,
jeeter
 

xadrieth

New Member
Messages
62
Reaction score
1
Points
0
Still learning PHP my self, but try:

PHP:
 //See if user is already logged in
  if ($_SESSION['loggedin'] == 1) //Logged in
  {
   //Store sessions userid into $name variable
   $name = $_SESSION['userid']; 

   //TODO: have username show in hyperlink..
   //Create hyperlink using $name variable
   echo '<td><a href="user.php">' . $name . 's User Panel</a></td>';
  }
  else //Not Logged in
  {
   echo '<td><a href="login.php">Login</a></td>';
  }
It probably has something to do with the quotes.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
make sure you have a running session. When I build session-based apps, I create a session class that contains only the function to start a session, and I include it in all the pages. Here is some sample code:
PHP:
function start($cookieName = 'php-session-handle', $time = 3600, $dir = dirname($_SERVER['PHP_SELF']) . '/') {
	$cookieName = str_replace(" ", "-", strtolower($cookieName));
	session_set_cookie_params($time, $dir);
	session_name($cookieName);
	session_start();

	 // Reset the expiration time upon page load
	if (isset($_COOKIE[$cookieName]) == true) {
		setcookie($cookieName, $_COOKIE[$cookieName], time() + $time, $dir);
 	}

  }
 
Last edited:

jeeter

New Member
Messages
9
Reaction score
0
Points
0
Yeah I think there is a problem with my Sessions. So do I create a page called like "sessions.php" and then in that put that code you posted, and then in each page call "require("sessions.php");"?

I believe this IS the problem because when I did some more error checking, I noticed that the $_SESSION['userid'] wasn't being set! I did an IF to check if the $_SESSION['userid'] was set and if not then store 'Blarg' into $name, and sure enough it stored 'Blarg' into $name and was able to show that in the hyperlink.

Thanks,
Jeeter
 

trike97

New Member
Messages
5
Reaction score
0
Points
0
Code:
<?php
    session_start();
    
    //if the session vars aren't set, try to set them with a cookie
    if (!isset($_SESSION['userID'])) {
        if (isset($_COOKIE['userID']) && isset($_COOKIE['userName'])) {
            $_SESSION['userID'] = $_COOKIE['userID'];
            $_SESSION['userName'] = $_COOKIE['userName'];
        }
    }//end ifs
?>

link that to every page that requires you to be logged in, assuming you use Sessions and Cookies together. Also, may think about just checking to see if it is set instead of
Code:
($_SESSION['loggedin'] == 1)


That makes sure it is set and eliminates an extra variable. In my code I log people in with both the user ID and user Name just to be safe
 

OdieusG

New Member
Messages
50
Reaction score
0
Points
0
Code:
<?php
    session_start();
    
    //if the session vars aren't set, try to set them with a cookie
    if (!isset($_SESSION['userID'])) {
        if (isset($_COOKIE['userID']) && isset($_COOKIE['userName'])) {
            $_SESSION['userID'] = $_COOKIE['userID'];
            $_SESSION['userName'] = $_COOKIE['userName'];
        }
    }//end ifs
?>
link that to every page that requires you to be logged in, assuming you use Sessions and Cookies together. Also, may think about just checking to see if it is set instead of
Code:
($_SESSION['loggedin'] == 1)


That makes sure it is set and eliminates an extra variable. In my code I log people in with both the user ID and user Name just to be safe

Technically, even if you do a conditional if($_SESSION['loggedin']) or if(!$_SESSION['userID']) it'll check to see if it has a value, if it's not set, it won't have any value to it....cookies are useless when you're using sessions, unless you want a more persistent connection.If you want them to login every time they visit it, just set the session variables, and it'll ask every time, since it'll be a different browser session.....
Edit:
Yeah I think there is a problem with my Sessions. So do I create a page called like "sessions.php" and then in that put that code you posted, and then in each page call "require("sessions.php");"?

I believe this IS the problem because when I did some more error checking, I noticed that the $_SESSION['userid'] wasn't being set! I did an IF to check if the $_SESSION['userid'] was set and if not then store 'Blarg' into $name, and sure enough it stored 'Blarg' into $name and was able to show that in the hyperlink.

Thanks,
Jeeter

CRAP! Didn't see this until I posted.....but use require_once instead of require, and it'll only use that file once, so if it somehow, gets nested in your code somewhere else, it won't call all the initialization variables you may set in sessions.php
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
IF you run into more problems, post the complete login script here.
 
Top