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
Naeem
Forum Commoner
Posts: 31 Joined: Tue Jul 07, 2009 12:48 pm
Post
by Naeem » Wed Aug 12, 2009 7:55 am
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%'> </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%'> </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
Eran
DevNet Master
Posts: 3549 Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME
Post
by Eran » Wed Aug 12, 2009 7:59 am
Fatal error: Cannot redeclare cleanstring()
You have the same function name declared more than once
Naeem
Forum Commoner
Posts: 31 Joined: Tue Jul 07, 2009 12:48 pm
Post
by Naeem » Wed Aug 12, 2009 8:15 am
How to solve that error?
Eran
DevNet Master
Posts: 3549 Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME
Post
by Eran » Wed Aug 12, 2009 8:17 am
change one of the function names so they don't collide?
Naeem
Forum Commoner
Posts: 31 Joined: Tue Jul 07, 2009 12:48 pm
Post
by Naeem » Wed Aug 12, 2009 8:51 am
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;
}
?>
Last edited by
Naeem on Wed Aug 12, 2009 9:16 am, edited 1 time in total.
mars_rahul
Forum Newbie
Posts: 12 Joined: Thu Aug 06, 2009 12:22 am
Post
by mars_rahul » Wed Aug 12, 2009 9:14 am
Will u please tell me what does "css();" does?
Do u have declared clearstring() funciton inside that.
All the other code is fine.
Naeem
Forum Commoner
Posts: 31 Joined: Tue Jul 07, 2009 12:48 pm
Post
by Naeem » Wed Aug 12, 2009 9:28 am
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;
}
mars_rahul
Forum Newbie
Posts: 12 Joined: Thu Aug 06, 2009 12:22 am
Post
by mars_rahul » Wed Aug 12, 2009 9:50 pm
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%'> </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%'> </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();
?>
aceconcepts
DevNet Resident
Posts: 1424 Joined: Mon Feb 06, 2006 11:26 am
Location: London
Post
by aceconcepts » Wed Aug 12, 2009 10:27 pm
It's quite obvious from the error message and just like pytrin also suggested.