...

View Full Version : PHP coding error



jonno4885
08-19-2011, 12:18 PM
Hey guys I keep getting an error on line 49 saying undefined index can anyone help me wit this?





<?php

$uri = $_SERVER['REQUEST_URI'];
function displaylogon($alert, $uri)
{

echo "<form method='post' action='login.php'>";

echo "Login Name" ;
echo "<input type='text' name='loginname' size='15'/>";
echo "Login Password";
echo "<input type='password' name='loginpassword' maxlength='15' size='15'/>";
echo "<span class='alert'>$alert</span>";
echo "<input type='submit' name='loginbutton' value='Login' />";
echo "</form>";

}
function displaywelcome($uri)
{
echo "<p>";

if($_SESSION['admin']==true)
{
echo "Welcome Administrator";
echo "<br/>";
echo "<br/>";
echo "<a href='../admin/admin.php'>Admin Page</a>";
}
else
{
echo "Welcome " . $_SESSION['firstname'];
echo "<br/>";
echo "<br/>";
echo "<a href='../main/cart.php'>View Cart</a>";
}
echo "</p>";

echo "<form action='../main/homepage.php' method='post'>";
echo "<table>";
echo "<tr>";
echo "<td><input type='hidden' value='true' name='logout' />";
echo "<input type='submit' value='Log out' /></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}

if($_POST['logout']==true)
{
//log the user out
session_destroy();

$alert = "You are now logged out.";
displaylogon($alert, $uri);
}
else
{

if (isset($_SESSION['username']))
{
if($_POST['logout']==true)
{
//log the user out
session_destroy();
header('location:..\homepage.php?');
//$alert = "You are now logged out.";
//displaylogon($alert, $uri);
}
else
{
displaywelcome($uri);
}
}
else if(isset($_POST['loginname']))
{
//check for admin login
if((strcmp("super",$_POST['loginname'])==0)&& (strcmp("super",$_POST['loginpassword'])==0))
{
//set up session variables
session_register('firstname');
session_register('username');
session_register('admin');
$_SESSION['admin']=true;
$_SESSION['firstname']= 'super';
$_SESSION['username']= 'super';

displaywelcome($uri);

}
else
{
//check user name is valid
@ $fp = fopen("data.txt","r");
if ($fp == null)
{
$alert = "An error has occurred, please try again.";
displaylogon($alert, $uri);
}
else
{
$match = false;
while (!feof($fp))
{
$line = fgetss($fp);
//only check if line from file is not blank.
if (!(($line =="") || ($line==null)))
{
$linearray = explode("\t",$line);
//check if username and password match
if((strnatcasecmp($linearray[2],$_POST['loginname'])==0)&& (strcmp($linearray[0],$_POST['loginpassword'])==0))
{
$match = true;
}
}
}

if ($match == true)
{
//set up session variables
session_register('firstname');
session_register('username');
$_SESSION['firstname']= $_POST['loginpassword'];
$_SESSION['username']= $_POST['loginname'];

displaywelcome($uri);


}
else
{
$alert = "Username or password is incorrect.";
displaylogon($alert, $uri);
}
fclose($fp);
}
}
}
else
{
//send the user back to where they came from
$alert="";
displaylogon($alert, $uri);


}
}
?>

gvre
08-19-2011, 01:39 PM
Replace this

if($_POST['logout']==true)
with this

if (isset($_POST['logout']) && $_POST['logout'] == "true")

sunfighter
08-19-2011, 01:52 PM
You should also start the sessions as your first line in this script.

tangoforce
08-19-2011, 03:07 PM
session_register is deprecated. Instead use $_SESSION['variable-name'].



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum