Jump to content

ok, need a second set of eyes here


desjardins2010

Recommended Posts

I have a simple login - logout script as it stands working fine.. I can loggin get the proper message, choose to logout get the proper message and all sessions are being destroyed proper..

 

problem is when I try to go back directly to the member.php page it's given me an error of

 

Notice: Undefined index: username in C:\wamp\www\protek\member.php on line 4

 

but still also telling me I have to be logged in to view this file

 

<?php
session_start();

if ($_SESSION['username']) {
	echo "Welcome, ".$_SESSION['username']."!<BR>";
	echo "<a href=\"logout.php\">LOGOUT</a>";
}
else {
die("Your Have To Be Logged In To View This Page");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/221060-ok-need-a-second-set-of-eyes-here/
Share on other sites

ok, sorry I jumped to conclusion too quick.. I'm not sure where to place the isset(); I had placed in first before the session_start and when refreshing it gave the proper message however now even when you do login you get the same message...

 

so question is where does an isset(); statement get placed in the above php

ERRRRRR.... dunno what I'm doing wrong?

do I have this in the right place?

 

<?php
if (isset($_SESSION['username'])) {
echo "Sorry you must be logged in to view this page<BR>";
echo "Please <a href=\"index.php\">GO BACK</a>and try again";
} 
  else
  {
session_start();


if ($_SESSION['username']) {
	echo "Welcome, ".$_SESSION['username']."!<BR>";
	echo "<a href=\"logout.php\">LOGOUT</a>";
}

?>

You must start the session first:

<?php
session_start();
if (isset($_SESSION['username'])) {
echo "Sorry you must be logged in to view this page<BR>";
echo "Please <a href=\"index.php\">GO BACK</a>and try again";
} 
  else
  {
if ($_SESSION['username']) {
echo "Welcome, ".$_SESSION['username']."!<BR>

echo "<a href=\"logout.php\">LOGOUT</a>";
}
?>

I should have read that, but you may want to change your code anyway:

 

<?php
session_start();
if (isset($_SESSION['username'])) {
echo "Welcome, ".$_SESSION['username']."!<BR>
echo "<a href=\"logout.php\">LOGOUT</a>";
} else {
echo "Sorry you must be logged in to view this page<BR>";
echo "Please <a href=\"index.php\">GO BACK</a>and try again";
}
?>

 

It just makes a bit more sense :)

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.