Jump to content

Recommended Posts

I have this simple sign in page. Sets a session for user and password. The problem is when I first load the page it shows the form and shows this error:

Notice Undefined index: username in ........on line 21

 

Then I type in a wrong user and password to test it and it clears the error and shows the "You are not logged in". message like it should.

The other problem is  my logout page. Seems simple it says you have been logged out, but it sends you back to the login page, shows that I am logged in and still shows the error: Notice Undefined index: username in ........on line 21

 

Login Page:

<?php session_start();

 

if ($dbc = @mysql_connect ('localhost', 'root', 'xxx')) {

if (!mysql_select_db ('phpbb')) {

die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');

}

 

} else {

die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');

}

 

// convert username and password from _POST or _SESSION

if($_POST){

  $_SESSION['username']=$_POST["username"];

  $_SESSION['password']=$_POST["user_password"];

 

}

 

// query for a user/pass match

$result=mysql_query("select * from phpbb_users

where username='" . $_SESSION['username'] . "' and user_password='" . $_SESSION['password'] . "'");

 

// retrieve number of rows resulted

$num=mysql_num_rows($result);

 

// print login form and exit if failed.

if($num < 1){

echo "You are not signed in.<br>

 

<form method=POST action=login.php>

  username: <input type=text name=\"username\">

  password: <input type=password name=\"user_password\">

  <input type=submit>

  </form>";

}

else {

echo "You are signed in.";

}

exit;

 

?>

---------------------------------------

 

Logout page:

<?php

session_start();

unset ($_SESSION['username']);

unset ($_SESSION['password']);

session_destroy();

 

echo "You have been successfully logged out.

 

<br><br>

You will now be returned to the login page.

 

<META HTTP-EQUIV=\"refresh\" content=\"2; URL=login.php\"> ";

?>

 

Link to comment
https://forums.phpfreaks.com/topic/70388-login-and-log-out-page-help/
Share on other sites

Notice Undefined index:

 

turn off notices in php.ini or you'll see this all the time.

undefined index means u are calling an index of an array which is not set

so if your gonna do something like print_r you cant see that index that you are trying to access

eg. session

print_r($_SESSION);

when destroying a session no need for the unset

Logout page:
<?php
session_start();
unset ($_SESSION['username']);
unset ($_SESSION['password']);
session_destroy();

simply

session_start();
session_destroy();

 

to check if the session is set

if (isset($_SESSION['username'])){
echo 'your logged';
}

 

I tried changing it to this and it still says i am logged in.

 

<?php

session_start();

session_destroy();

 

echo "You have been successfully logged out.

 

<br><br>

You will now be returned to the login page.

 

<META HTTP-EQUIV=\"refresh\" content=\"2; URL=login.php\"> ";

?>

 

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.