Help 'Undefined Index'

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Help 'Undefined Index'

Post by Getran »

I keep getting an 'Undefined Index' error with my login.php file. this is the message:


Notice: Undefined index: terrastormlogin in *hidden*\login.php on line 2

this is the bit on line 2:

$checkcookie = $_COOKIE['terrastormlogin'];

Can someone tell me how to stop this error ?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Are you by chance setting is as "checkcookie", yet accessing it as "terrastormlogin"?
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

this is how i'm using it:

Code: Select all

<?php
	$checkcookie = $_COOKIE['terrastormlogin'];
    if (isset($checkcookie))
    {
    	$loggedin = "1";
			global $loggedin;
			global $user;
			$maincontent = "";
			$maincontent .= "You are already logged in!<br>";
      $maincontent .= "Would you like to <a href='logout.php'>Logout</a> ?";
    }
    else if (!isset($checkcookie))
    {
    	$loggedin = "0";
			global $loggedin;
			$maincontent = "";
			$maincontent .= "Enter your username and password to login!";
			$maincontent .= "<form action='login.php' method='POST'>";
			$maincontent .= "<input type='text' name='user' value='Username'><br>";
			$maincontent .= "<input type='text' name='pass' value='Password'><br>";
			$maincontent .= "<input type='submit' name='login' value='Login'><br>";
			$maincontent .= "</form>";
    }
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

try..

Code: Select all

<?php
    if (isset($_COOKIE['terrastormlogin']))
    {
       $loggedin = "1";
         global $loggedin;
         global $user;
         $maincontent = "";
         $maincontent .= "You are already logged in!<br>";
      $maincontent .= "Would you like to <a href='logout.php'>Logout</a> ?";
    }
    else if (!isset($checkcookie))
    {
       $loggedin = "0";
         global $loggedin;
         $maincontent = "";
         $maincontent .= "Enter your username and password to login!";
         $maincontent .= "<form action='login.php' method='POST'>";
         $maincontent .= "<input type='text' name='user' value='Username'><br>";
         $maincontent .= "<input type='text' name='pass' value='Password'><br>";
         $maincontent .= "<input type='submit' name='login' value='Login'><br>";
         $maincontent .= "</form>";
    }
?>
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

ok that worked lol thx man. I have a few more things i need help on (and i thought it's better to post here than make a new topic).

i get this error:

Notice: Undefined offset: 9 in *hidden*\random.php on line 9

line 9 = $RandomRotator1 = ereg_replace("\n","",$file[$randcount]);

This is the contents of the random.php file:


Code: Select all

<?php
$RandomRotator1 = "";
$filename = "random.txt";
$file = file($filename);
srand((double)microtime()*1000000);
while ($RandomRotator1 == "") {
if(!isset($sp)){
$randcount = rand(0,count($file));
$RandomRotator1 = ereg_replace("\n","",$file[$randcount]);
}else{
$RandomRotator1 = ereg_replace("\n","",$file[$sp]);
}
}
print "$RandomRotator1";
?>
I thought i solved this problem once, but it's come back to haunt me :roll:

And plus, with my login script, i have to login tiwce for it to actually show you "You have been logged in" message... i don't know why.

This is my login code:

Code: Select all

<?php
if (isset($login))
{
 $login = "";
 $user = $_POST['user'];
 $pass = $_POST['pass'];
 require("db_info.php");
 mysql_connect("$host", "$username", "$password");
 mysql_select_db("$database");
 $loginquery = mysql_query("SELECT * from accounts WHERE username='$user'and password='$pass'") or die("ERROR: Username and password not found or not matched!");
 $worked = mysql_fetch_array($loginquery);

 $user = $worked['username'];
 $pass = $worked['password'];

 if ($worked)
 {
	setcookie("terrastormlogin", "$user", time() + 86400);
	global $user;
 	$content = "Login Successful!<br>";
  $content .= "Welcome, " . $user . ", you will be redirected in 2 seconds!";
	$content .= "<meta http-equiv='refresh' content='2;url=portal.php'>";
 }
 else if (!$worked)
 {
 	$maincontent .= "Login failed!";
    $loggedin = "0";
 }
 else
 {
 	$loggedin = "0";
 }
}
else
{
	$login = "";
    $loggedin = "0";
    $content = "";
}
	$login = "";
?>
Post Reply