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
Kilgore Trout
Forum Newbie
Posts: 10 Joined: Sun Oct 30, 2005 3:40 pm
Location: MI
Post
by Kilgore Trout » Mon Nov 07, 2005 4:08 pm
I have a simple login form that when run on my local server works great in checking the database for the username and encrypted password, then redirecting to the desired url. I’m trying to implement this on the real server (1&1) but am having problems with the redirection. After posting I get into the ‘if’ conditional where it should be redirecting, but instead i get a blank screen with my test echo. I’ve tried various file references such as ./file.php and ../file.php and /file.php and finally the entire
http://www.url.com./file.php with the same lack of results. My knowledge of headers is perfunctory, but before I do some unnecessary in depth study I thought I’d asks the gurus, what am I missing? Any ideas?
Code: Select all
<?php
session_start();
require ($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
$connection = @mysql_connect($db_host, $db_user, $db_password) or die("error connecting");
mysql_select_db($db_name, $connection);
require ($_SERVER["DOCUMENT_ROOT"]."/functions.php");
if ($_POST){
$error = login_check($_POST);
if (trim($error) == ""){
$_SESSION["member_id"] = login($_POST);
echo "trying to redirect.";
Header("Location: ./propertyInput.php");
exit();
}else{
print "Error:$error";
}
}
?>
<html>
<head>
<title>Property Input Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>
<center><div align="center">
<table width="100%" height="100%">
<tr>
<td align="center" valign="middle">
<form action="<?php $_SERVER[PHP_SELF] ?>" method="post" enctype="multipart/form-data">
<label for="userid">Username</label>
<input type="text" name="username" id="username" />
<br />
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<br />
<input type="submit" name="submit" value="Login" />
</form>
</td>
</tr>
</body>
</html>
yum-jelly
Forum Commoner
Posts: 98 Joined: Sat Oct 29, 2005 9:16 pm
Post
by yum-jelly » Mon Nov 07, 2005 4:13 pm
You can not output anything before the location header, comment this...
Or just remove it! And you dont need the dot before the URL in the header!
Code: Select all
header ( 'Location: /propertyInput.php' );
yj
Kilgore Trout
Forum Newbie
Posts: 10 Joined: Sun Oct 30, 2005 3:40 pm
Location: MI
Post
by Kilgore Trout » Mon Nov 07, 2005 4:27 pm
Code: Select all
if ($_POST){
$error = login_check($_POST);
if (trim($error) == ""){
$_SESSION["member_id"] = login($_POST);
Header('Location: /propertyInput.php');
exit();
}else{
print "Error:$error";
}
}
just gets me a blank page and the url in the address bar stays on the login.php page.
the functions used are...
Code: Select all
function login_check($forms){
require ($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
$connection = @mysql_connect($db_host, $db_user, $db_password) or die("error connecting");
mysql_select_db($db_name, $connection);
$error = "";
$username = $forms["username"];
$password = sha1($forms["password"]);
if (trim($username) == ""){
$error .= "<li>You forgot to enter in your username.</li>";}
if (trim($password) == ""){
$error .= "<li>You forgot to enter in a password.</li>";}
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysql_query($query, $connection) or die(mysql_error());
if (!(mysql_fetch_row($result))){
$error .= "<li>Username and/or Password is invalid.</li>";
}
if (trim($error) != ""){
return $error;}
}
function login($forms){
require ($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
$connection = @mysql_connect($db_host, $db_user, $db_password) or die("error connecting");
mysql_select_db($db_name, $connection);
$username = $forms["username"];
$password = sha1($forms["password"]);
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysql_query($query, $connection) or die(mysql_error());
if (mysql_fetch_row($result)){
$member_id = "granted";
return $member_id;
}
}
i don't
think anything is being sent out before the header. thanks for the quick reply by the way.
Kilgore Trout
Forum Newbie
Posts: 10 Joined: Sun Oct 30, 2005 3:40 pm
Location: MI
Post
by Kilgore Trout » Mon Nov 07, 2005 5:15 pm
I made a test redirect.php file on my server
Code: Select all
<?php
Header('Location: /propertyInput.php');
?>
and it works. i can only assume that i'm outputing something before the header but i don't see where. is there another way to redirect in php?
foobar
Forum Regular
Posts: 613 Joined: Wed Sep 28, 2005 10:08 am
Post
by foobar » Mon Nov 07, 2005 5:25 pm
Kilgore Trout wrote: I made a test redirect.php file on my server
Code: Select all
<?php
Header('Location: /propertyInput.php');
?>
and it works. i can only assume that i'm outputing something before the header but i don't see where. is there another way to redirect in php?
Well, you can always
include() another script/page wherever you want it to appear.
Alternatively, you can use HTML/JS:
Code: Select all
<meta http-equiv="Refresh" value="0; blabla.html" />
...or...
<script type="text/javascript">window.onload=function(){window.location='fartnoodles.php';}</script>
duk
Forum Contributor
Posts: 199 Joined: Wed May 19, 2004 8:45 am
Location: London
Post
by duk » Mon Nov 07, 2005 5:33 pm
why re you using a dot before " / " ???
anywya if there is no problems with the dot, if you assume that maybe you are outputing something to the header use ob_start(); after session_start();
Kilgore Trout
Forum Newbie
Posts: 10 Joined: Sun Oct 30, 2005 3:40 pm
Location: MI
Post
by Kilgore Trout » Mon Nov 07, 2005 6:33 pm
I've isolated the problem to the require statement
Code: Select all
require ($_SERVER["DOCUMENT_ROOT"]."/config/db_config.php");
this file includes only four variable definations.
Code: Select all
<?php
$db_host = "hostname.net";
$db_user = "username";
$db_password = "password";
$db_name = "databasename";
?>
if i replace the require statement with these four lines, then the redirect header works. The other require statement requiring functions.php works just fine. the only difference is that db_config.php is located in another folder as I plan on making that folder restricted to hide the database info. Why on Earth would defining those variables in a require statement have an effect on a header statement?
Kilgore Trout
Forum Newbie
Posts: 10 Joined: Sun Oct 30, 2005 3:40 pm
Location: MI
Post
by Kilgore Trout » Mon Nov 07, 2005 8:13 pm
After much frustration, i took a break for food, made some coffee, came back and found that the cause for the header not working was four blank returns AFTER my ?> tag in the db_config.php file that I was requiring. I guess when the file was included it included the blank returns as output, which doesn't show up on a blank white screen or in an ob_start buffer. I love programming.