Page 1 of 1

Help with login please!

Posted: Sat Mar 12, 2011 4:54 pm
by simmsy
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>

Re: Help with login please!

Posted: Sat Mar 12, 2011 5:01 pm
by jankidudel
1. - Use code brackets.
2. - Use code brackets.
3. - Use code brackets.

No one wants to read that mess.

Re: Help with login please!

Posted: Sat Mar 12, 2011 5:42 pm
by simmsy
well the page just takes me to the blank login.php page and doesn't take me to the main.php with the welcome $username message on it? just need help with it to see where im going wrong?

Re: Help with login please!

Posted: Sat Mar 12, 2011 6:34 pm
by jankidudel
Next time just be sure you are using code tags.

Code: Select all

<?php
session_start();
if(!isset($_SESSION['logged']))
{
	if(empty($_POST['username']) || empty($_POST['password']))
	{
		?>
		<form>
		</form>
		<?php
	}
	else
	{
		// code to check if such user exists;
		if(user_exists)
		{
			header("Location: main.php");
		}
	}
}
else
{
	header("Location: main.php");
}
?>

Re: Help with login please!

Posted: Sat Mar 12, 2011 7:43 pm
by simmsy
this still doesnt really apply to the script and still doesnt work any other ideas?

Re: Help with login please!

Posted: Sat Mar 12, 2011 8:01 pm
by jankidudel
It's because you can' do it yourself. It's just an example. People , sitting here won't read your code, written in that manner. If you feel that you can't do it yourself, just use scripts that already exists .

Re: Help with login please!

Posted: Sat Mar 12, 2011 8:39 pm
by simmsy
this is a script used already but tried to modify it into my script so I can use the design of my site while still welcoming the login user!!!

Re: Help with login please!

Posted: Sun Mar 13, 2011 6:39 am
by simmsy
all sorted now was just a mishap with the name of the button!