Page 1 of 1

Fatal error

Posted: Wed Aug 12, 2009 7:55 am
by Naeem
Hello friends,
I am sure you will be fine.
Todays i am learning php and have made a simple login system:
here it is:

Code: Select all

<?php
ob_start();
session_start();
include "./inc/functions.php";
include "./inc/connection.php";
echo css();
if ($_SESSION['logged'] == "yes") { //If session is true then redirect to guestbook.
    header("Location: guestbook.php");
    exit();
} else {
    echo "<center>";
    echo "<table border='1' width='50%'>";
    echo "<form method='POST' action='login.php'>";
    echo "<tr><td width='50%'><b>Username:</b></td><td width='50%'><input type='text' name='username' /></td></tr>";
    echo "<tr><td width='50%'><b>Password:</b></td><td width='50%'><input type='password' name='password'></td></tr>";
    echo "<tr><td width='50%'>&nbsp;</td><td width='50%'><b>Remember Me:</b><input type='checkbox' name='remember_me' value='on' /></td></tr>";
    echo "<input type='hidden' name='post' value='true' />";
    echo "<tr><td width='50%'>&nbsp;</td><td><input type='submit' value='Login' /></td></tr>";
    echo "</form>";
    echo "</table>";
    echo "</center>";
    //Now getting form values
    if (isset($_POST['post'])) {
        $username = cleanString($_POST['username']); //Getting username
        $password = cleanString(md5($_POST['password'])); //Getting Password
        $remember_me = $_POST['remember_me']; //Getting to create cookies
        $query = "SELECT * FROM member WHERE username = '$username' AND password = '$password'";
        $result = mysql_query($query);
        if($remember_me == "on"){
            setcookie("username",$username,time()+3600);
            setcookie("password",$password,time()+3600);        
        }
        $check_user = mysql_num_rows($result);
        if ($check_user > 0) {
            $_SESSION['logged'] = "yes";
            $_SESSION['user'] = $username;
            header("Location: guestbook.php");
                exit();
            }
         else {
            echo "<center><b>Wrong Username OR Password</b></center>";
            exit();
        }
    }
}
ob_end_flush();
?>
I have use a header in it which can redirect a user to guestbook.php but whenever user clear a cookies it send an error:
here it is:

Code: Select all

Fatal error: Cannot redeclare cleanstring() (previously declared in C:\xampp\htdocs\php\inc\functions.php:2) in C:\xampp\htdocs\php\inc\functions.php on line 7

Re: Fatal error

Posted: Wed Aug 12, 2009 7:59 am
by Eran
Fatal error: Cannot redeclare cleanstring()
You have the same function name declared more than once

Re: Fatal error

Posted: Wed Aug 12, 2009 8:15 am
by Naeem
How to solve that error?

Re: Fatal error

Posted: Wed Aug 12, 2009 8:17 am
by Eran
change one of the function names so they don't collide?

Re: Fatal error

Posted: Wed Aug 12, 2009 8:51 am
by Naeem
Here is a function file:What to do in this to get rid of this error:

Code: Select all

<?php
function cleanString($string){
$cstr = htmlspecialchars($string);
$cstr = trim($cstr);
$cstr = addslashes($cstr);
return $cstr;
}
?>

Re: Fatal error

Posted: Wed Aug 12, 2009 9:14 am
by mars_rahul
Will u please tell me what does "css();" does?

Do u have declared clearstring() funciton inside that.

All the other code is fine.

Re: Fatal error

Posted: Wed Aug 12, 2009 9:28 am
by Naeem
This is css(); function:

Code: Select all

function css(){
$css = "<style type='text/css'>
body{
font-size:0.92em;
font-family: arial,tahoma,verdana;
padding:50px;
margin:50px;
border:25px;
}
table{
border:1px solid silver;
border-collapse: collapse;
}
td{
font-size:0.9em;
font-family: arial,tahoma,verdana;
border:1px solid silver;
border-collapse: collapse;
}
input{
font-size:0.85em;
font-family: arial,tahoma,verdana;
border:2px inset silver;
}
</style>";
return $css;
}

I am sorry Friend, I didn't find mistake and some code modif

Posted: Wed Aug 12, 2009 9:50 pm
by mars_rahul
User This code instead of your. hope it will work.
And
Please check to connection.php file, that page must not echo anything before this page echoed

<?php
error_reporting(E_ALL);
ob_start();
session_start();
include "./inc/functions.php";
include "./inc/connection.php";
echo css();

if (isset($_SESSION['logged'])) { //If session is true then redirect to guestbook.
header("Location: guestbook.php");
exit();
} else {
echo "<center>";
echo "<table border='1' width='50%'>";
echo "<form method='POST' action='login.php'>";
echo "<tr><td width='50%'><b>Username:</b></td><td width='50%'><input type='text' name='username' /></td></tr>";
echo "<tr><td width='50%'><b>Password:</b></td><td width='50%'><input type='password' name='password'></td></tr>";
echo "<tr><td width='50%'>&nbsp;</td><td width='50%'><b>Remember Me:</b><input type='checkbox' name='remember_me' value='on' /></td></tr>";
echo "<input type='hidden' name='post' value='true' />";
echo "<tr><td width='50%'>&nbsp;</td><td><input type='submit' value='Login' /></td></tr>";
echo "</form>";
echo "</table>";
echo "</center>";
//Now getting form values
if (isset($_POST['post'])) {
$username = cleanString($_POST['username']); //Getting username
$password = cleanString(md5($_POST['password'])); //Getting Password
$remember_me = $_POST['remember_me']; //Getting to create cookies
$query = "SELECT * FROM member WHERE username = '$username' AND password = '$password'";
$result = mysql_query($query);
if($remember_me == "on"){
setcookie("username",$username,time()+3600);
setcookie("password",$password,time()+3600);
}
$check_user = mysql_num_rows($result);
if ($check_user > 0) {
$_SESSION['logged'] = "yes";
$_SESSION['user'] = $username;
header("Location: guestbook.php");
exit();
}
else {
echo "<center><b>Wrong Username OR Password</b></center>";
exit();
}
}
}
ob_end_flush();
?>

Re: Fatal error

Posted: Wed Aug 12, 2009 10:27 pm
by aceconcepts
It's quite obvious from the error message and just like pytrin also suggested.