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
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

Link to comment
Share on other sites

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>";
}

?>

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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 :)

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.