Help with login please!
Posted: Sat Mar 12, 2011 4:54 pm
Hi ive been trying to get this login to work but it doesn't seem to work please help:
login.php:
<?php
//ob
ob_start();
//session
session_start();
if (isset($_SESSION['username']))
{
header("Location: main.php");
exit();
}
//connect
$error = 'Could not connect to the database';
mysql_connect('localhost','************','***************') or die($error);
mysql_select_db('********') or die($error);
if ($_POST['login'])
{
//get form data
$username = addslashes(strip_tags(strtolower($_POST['username'])));
$password = addslashes(strip_tags(md5($_POST['password'])));
if (!$username||!$password)
echo "Please enter a username and password<p />";
else
{
//find username
$find = mysql_query("SELECT * FROM ********** WHERE username='$username' AND password='$password'");
if (mysql_num_rows($find)==0)
echo "Username or password is incorrect<p />";
else
{
$_SESSION['username']=$username;
header("Location: main.php");
exit();
}
}
}
?>
main.php
<?
// You may copy this PHP section to the top of file which needs to access after login.
session_start(); // Use session variable on this page. This function must put on the top of page.
$username = $_SESSION['username']
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel=StyleSheet href="style.css" type="text/css" media=screen>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="nomargin"><img src="banner.jpg" alt="" /></div>
<div id="login"><form name="login" method="post" action="login.php">
<table border="0" width="220" align="center">
<tr>
<? if(!session_is_registered("username")){ // if session variable "username" does not exist.
echo "<td width='71'>Username:</td>
<td width='139'><input type='text' name='username' size='16' style='font-family: Times New Roman, Times, serif; font-size: 11pt;' /></td>
</tr>
<tr>
<td width='71'>Password:</td>
<td width='139'><input type='password' name='password' size='16' style='font-family: Times New Roman, Times, serif; font-size: 11pt;' /></td>
</tr>
<tr>
<td colspan='2' align='center'><input name='signup' type='submit' value='Log In' style='background-color: #900002; border-color: #555555; color: #FFFFFF; font-weight: bold; width: 60px;' /></td>";
}else{
echo "<td><b>Welcome $username</b><br /><a href='logout.php'><b>Logout</b></a></td>";
}
?>
</tr>
</table>
</form></div>
<div id="menutext"><span class="class1"><a href="**************">Home</a> <a href="http://www.fightwatcher.com">News</a> <a href="http://www.fightwatcher.com">Profile</a> <a href="http://www.fightwatcher.com">Friends & Fighters</a> <a href="http://www.fightwatcher.com">Videos</a> <a href="http://www.fightwatcher.com">Groups</a> <a href="http://www.fightwatcher.com">Forum</a></span></div>
</body>
</html>
login.php:
<?php
//ob
ob_start();
//session
session_start();
if (isset($_SESSION['username']))
{
header("Location: main.php");
exit();
}
//connect
$error = 'Could not connect to the database';
mysql_connect('localhost','************','***************') or die($error);
mysql_select_db('********') or die($error);
if ($_POST['login'])
{
//get form data
$username = addslashes(strip_tags(strtolower($_POST['username'])));
$password = addslashes(strip_tags(md5($_POST['password'])));
if (!$username||!$password)
echo "Please enter a username and password<p />";
else
{
//find username
$find = mysql_query("SELECT * FROM ********** WHERE username='$username' AND password='$password'");
if (mysql_num_rows($find)==0)
echo "Username or password is incorrect<p />";
else
{
$_SESSION['username']=$username;
header("Location: main.php");
exit();
}
}
}
?>
main.php
<?
// You may copy this PHP section to the top of file which needs to access after login.
session_start(); // Use session variable on this page. This function must put on the top of page.
$username = $_SESSION['username']
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel=StyleSheet href="style.css" type="text/css" media=screen>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="nomargin"><img src="banner.jpg" alt="" /></div>
<div id="login"><form name="login" method="post" action="login.php">
<table border="0" width="220" align="center">
<tr>
<? if(!session_is_registered("username")){ // if session variable "username" does not exist.
echo "<td width='71'>Username:</td>
<td width='139'><input type='text' name='username' size='16' style='font-family: Times New Roman, Times, serif; font-size: 11pt;' /></td>
</tr>
<tr>
<td width='71'>Password:</td>
<td width='139'><input type='password' name='password' size='16' style='font-family: Times New Roman, Times, serif; font-size: 11pt;' /></td>
</tr>
<tr>
<td colspan='2' align='center'><input name='signup' type='submit' value='Log In' style='background-color: #900002; border-color: #555555; color: #FFFFFF; font-weight: bold; width: 60px;' /></td>";
}else{
echo "<td><b>Welcome $username</b><br /><a href='logout.php'><b>Logout</b></a></td>";
}
?>
</tr>
</table>
</form></div>
<div id="menutext"><span class="class1"><a href="**************">Home</a> <a href="http://www.fightwatcher.com">News</a> <a href="http://www.fightwatcher.com">Profile</a> <a href="http://www.fightwatcher.com">Friends & Fighters</a> <a href="http://www.fightwatcher.com">Videos</a> <a href="http://www.fightwatcher.com">Groups</a> <a href="http://www.fightwatcher.com">Forum</a></span></div>
</body>
</html>