PDA

View Full Version : Login Form as an Include


Irascibleone
11-23-2008, 11:28 PM
I have just recently gone through the login tutorial and it works perfectly when it is alone on its own page. However, when I try to use it as an include on my home page it does not work at all. I can't figure out why this won't work but I know for a fact that the problem arises from using at as an include.

Below is the coding my login form. When I test it out on my home page it always fails at "if($row)". For some reason it doesn't want to recognize that it is true, when it is. So it always tells me I have a bad username or password.

<?php
include('connect.php');
session_start();

$username = $_POST['username'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if($username && $password){
$query = sprintf("SELECT * FROM users WHERE username='$username' and userpass='$password'");
$result = mysql_query($query);
$row = mysql_fetch_array($result);
}

if($row){
$_SESSION['id'] = $row['username'];
}elseif($submitted){
echo "Your Username or Password is incorrect";
}

if(!isset($_SESSION['id'])){
echo '<form method="post" action="login.php" >
<table width="250px" cellpadding="2px" cellspacing="0px">
<tr><td>Username: </td><td><input class="fields" type="text" name="username" /></td></tr>
<tr><td>Password: </td><td><input class="fields" type="password" name="password" /></td></tr>
<tr><td><input type="hidden" name="submitted" value="1" /></td>
<td><input type="submit" value="Login" style="float: right; margin-right: 20px;"/></td></tr>
</table>
</form>';
}else {
echo '<div class="logintext">Hello, ' . $_SESSION['id'] . '<br /> We are glad you have joined us!<br />
<a href=logout.php">Logout</a></div>';
}
?>

edbr
11-24-2008, 12:08 AM
is the path to your connect .php correct when it is an include ie is it in the same folder as your page with the included form?

Irascibleone
11-24-2008, 01:54 AM
Yes and I have managed to make it work. A few moments after posting the initial post I actually figured it out. (That seems to happen a lot to me...)

I realized that my login.php was included in a file (header.php) that was included on the home page. I decided I would take everything from the login.php and actually put it into the header.php file rather than using an include. After that everything started to work. Even so, I don't understand why that would change anything. I thought includes worked the same way as actually putting the code in its place.

Anyways, It works so I'm not going to mess with it.

2bz2p
11-26-2008, 01:28 PM
Just a thought, You can make your login form post to the login.php file by


<form name="loginform" method="post" action="login.php">


This way regardles of where your form is it will alwas post to that file. THis is true for any forms.

2b

EDIT: I didnt realize you had the html code in you log in php file. My example is for using say login.html (which holds the html) and posting it to login.php (which catches the values from the log in form).