php redirect doesnt work
am 08.10.2007 03:21:21 von jack
Hi,
I have a problem that I've been stuck on for while now.
Here is my code
Login
Login
if ( (isset($_POST['uname'])) && (isset($_POST['pass'])) ){
include '../dbConn.inc';
$uname = $_POST['uname'];
$pass = sha1($_POST['pass']);
$dbResult = mysql_query("select uid from users where
username='$uname' and password='$pass'", $dbh);
mysql_close($dbh);
if (mysql_num_rows($dbResult) < 1){
print "Invalid username or password.";
}else{
session_start();
$_SESSION['uid'] = mysql_result($dbResult, 0, 'uid');
header('Location: main.php');
}
}else{
?>
}
?>
For some reason, the page doesn't get redirected to main.php. Can
anyone help me with this.
ps. there is no spaces after ?> at the bottom of the code.
Thanks
Re: php redirect doesnt work
am 08.10.2007 04:43:54 von Jerry Stuckle
Jack wrote:
> Hi,
> I have a problem that I've been stuck on for while now.
> Here is my code
>
Jack,
First of all, enable all errors and display errors in your PHP.INI file.
If you don't have access to this file, you can use ini_set to set
them, but it's much easier to do it in the php.ini file (also this way
works even if you have syntax errors in your code).
Once you get this, I'm pretty sure you'll get a message about "header
already sent...". I don't remember the exact message off hand (you'd
think I would, the number of times I've seen it! :-) ), but it comes
because you've send output before this time. It could only be blank
space - but you've sent SOME data.
And you should also get a message indicating "output started in file xxx
at line yyy". This is where you need to took to find why output has
previously been sent.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: php redirect doesnt work
am 08.10.2007 05:15:45 von Wings
"Jerry Stuckle" wrote in message
news:KoadnU6EQZ83BJTanZ2dnUVZ_vamnZ2d@comcast.com...
> Jack wrote:
>> Hi,
>> I have a problem that I've been stuck on for while now.
>> Here is my code
>>
>
>
> Jack,
>
> First of all, enable all errors and display errors in your PHP.INI file.
> If you don't have access to this file, you can use ini_set to set them,
> but it's much easier to do it in the php.ini file (also this way works
> even if you have syntax errors in your code).
>
> Once you get this, I'm pretty sure you'll get a message about "header
> already sent...". I don't remember the exact message off hand (you'd
> think I would, the number of times I've seen it! :-) ), but it comes
> because you've send output before this time. It could only be blank
> space - but you've sent SOME data.
>
> And you should also get a message indicating "output started in file xxx
> at line yyy". This is where you need to took to find why output has
> previously been sent.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================
Jerry,
I'm not a coder, but in installing php scripts I've run into that "header
already sent" quite a number of times. For me, a non-coder, that meant I had
to dispense with the script and go look for another.
Do you know a reason or two "why?" - something that a non-coder might find
and fix?
Thanks in advance.
Re: php redirect doesnt work
am 08.10.2007 06:17:16 von Shion
Jack wrote:
> Hi,
> I have a problem that I've been stuck on for while now.
> Here is my code
>
>
>
You see this HTML that is on the top of your page, it will prevent the
header() from work, as it has to be sent before any other output from a script.
You can try with adding ob_start() as the first thing on the page.
--
//Aho
Re: php redirect doesnt work
am 08.10.2007 08:40:19 von David Quinton
On Mon, 08 Oct 2007 06:17:16 +0200, "J.O. Aho"
wrote:
>You see this HTML that is on the top of your page, it will prevent the
>header() from work, as it has to be sent before any other output from a script.
I agree.
Move this stuff:
Login
Login
so it's above the opening form tag.
one other thing you *might* have to watch out for is:-
"Note: HTTP/1.1 requires an absolute URI as argument to ยป Location:
including the scheme, hostname and absolute path, but some clients
accept relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself: "
(from )
--
Locate your Mobile phone:
Great gifts:
Re: php redirect doesnt work
am 08.10.2007 13:55:39 von Jerry Stuckle
Wings wrote:
> "Jerry Stuckle" wrote in message
> news:KoadnU6EQZ83BJTanZ2dnUVZ_vamnZ2d@comcast.com...
>> Jack wrote:
>>> Hi,
>>> I have a problem that I've been stuck on for while now.
>>> Here is my code
>>>
>>
>>
>> Jack,
>>
>> First of all, enable all errors and display errors in your PHP.INI file.
>> If you don't have access to this file, you can use ini_set to set them,
>> but it's much easier to do it in the php.ini file (also this way works
>> even if you have syntax errors in your code).
>>
>> Once you get this, I'm pretty sure you'll get a message about "header
>> already sent...". I don't remember the exact message off hand (you'd
>> think I would, the number of times I've seen it! :-) ), but it comes
>> because you've send output before this time. It could only be blank
>> space - but you've sent SOME data.
>>
>> And you should also get a message indicating "output started in file xxx
>> at line yyy". This is where you need to took to find why output has
>> previously been sent.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstucklex@attglobal.net
>> ==================
>
> Jerry,
> I'm not a coder, but in installing php scripts I've run into that "header
> already sent" quite a number of times. For me, a non-coder, that meant I had
> to dispense with the script and go look for another.
> Do you know a reason or two "why?" - something that a non-coder might find
> and fix?
> Thanks in advance.
>
>
It means you've sent output (even white space) to the client before the
call to session_start() or similar call.
If you want to play with PHP, I highly recommend you learn some of the
basics. No language is completely "plug and play". There will be
problems at times, but most do not require an expert to fix. They do,
however, require someone with knowledge of the language.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: php redirect doesnt work
am 09.10.2007 06:35:24 von Wings
>> Jerry,
>> I'm not a coder, but in installing php scripts I've run into that "header
>> already sent" quite a number of times. For me, a non-coder, that meant I
>> had to dispense with the script and go look for another.
>> Do you know a reason or two "why?" - something that a non-coder might
>> find and fix?
>> Thanks in advance.
>
> It means you've sent output (even white space) to the client before the
> call to session_start() or similar call.
>
> If you want to play with PHP, I highly recommend you learn some of the
> basics. No language is completely "plug and play". There will be
> problems at times, but most do not require an expert to fix. They do,
> however, require someone with knowledge of the language.
>
Thanks for the info and advise.
Re: php redirect doesnt work
am 26.10.2007 18:47:56 von aaron.forsander
On Oct 7, 8:21 pm, Jack wrote:
> Hi,
> I have a problem that I've been stuck on for while now.
> Here is my code
>
>
>
> Login
>
>
> Login
>
> if ( (isset($_POST['uname'])) && (isset($_POST['pass'])) ){
> include '../dbConn.inc';
> $uname = $_POST['uname'];
> $pass = sha1($_POST['pass']);
>
> $dbResult = mysql_query("select uid from users where
> username='$uname' and password='$pass'", $dbh);
> mysql_close($dbh);
> if (mysql_num_rows($dbResult) < 1){
> print "Invalid username or password.";
> }else{
> session_start();
> $_SESSION['uid'] = mysql_result($dbResult, 0, 'uid');
> header('Location: main.php');
> }}else{
>
> ?>
>
>
>
>
>
>
> ?>
>
> For some reason, the page doesn't get redirected to main.php. Can
> anyone help me with this.
>
> ps. there is no spaces after ?> at the bottom of the code.
>
> Thanks
You've already sent the HTTP headers. You can't send them twice. You
need to call 'header()' BEFORE you output anything or else it will not
work.