PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > php redirect doesnt work
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
php redirect doesnt work

Réponse
 
LinkBack Outils de la discussion
Vieux 08/10/2007, 02h21   #1 (permalink)
Jack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut php redirect doesnt work

Hi,
I have a problem that I've been stuck on for while now.
Here is my code

<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<?php
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{
?>
<form method="post" action="login.php">
<table border="0">
<tr>
<td>Username:</td>
<td><input type="text" name="uname" size="20"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass" size="20"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="login"/></td>
</tr>
</table>

</form>

</body>
</html>
<?php
}
?>

For some reason, the page doesn't get redirected to main.php. Can
anyone me with this.

ps. there is no spaces after ?> at the bottom of the code.

Thanks

  Réponse avec citation
Vieux 08/10/2007, 03h43   #2 (permalink)
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work

Jack wrote:
> Hi,
> I have a problem that I've been stuck on for while now.
> Here is my code
>

<snipped>

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
==================
  Réponse avec citation
Vieux 08/10/2007, 04h15   #3 (permalink)
Wings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work


"Jerry Stuckle" <jstucklex@attglobal.net> 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
>>

> <snipped>
>
> 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.


  Réponse avec citation
Vieux 08/10/2007, 05h17   #4 (permalink)
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work

Jack wrote:
> Hi,
> I have a problem that I've been stuck on for while now.
> Here is my code
>
> <html>
> <head>


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
  Réponse avec citation
Vieux 08/10/2007, 07h40   #5 (permalink)
David Quinton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work

On Mon, 08 Oct 2007 06:17:16 +0200, "J.O. Aho" <user@example.net>
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:
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>

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 <http://uk3.php.net/header>)
--
Locate your Mobile phone: <http://www.bizorg.co.uk/news.html>
Great gifts: <http://www.ThisBritain.com/ASOS_popup.html>
  Réponse avec citation
Vieux 08/10/2007, 12h55   #6 (permalink)
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work

Wings wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> 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
>>>

>> <snipped>
>>
>> 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
==================
  Réponse avec citation
Vieux 09/10/2007, 05h35   #7 (permalink)
Wings
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work



>> 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.


  Réponse avec citation
Vieux 26/10/2007, 17h47   #8 (permalink)
aaron.forsander@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: php redirect doesnt work

On Oct 7, 8:21 pm, Jack <accpac...@hotmail.com> wrote:
> Hi,
> I have a problem that I've been stuck on for while now.
> Here is my code
>
> <html>
> <head>
> <title>Login</title>
> </head>
> <body>
> <h2>Login</h2>
> <?php
> 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{
>
> ?>
> <form method="post" action="login.php">
> <table border="0">
> <tr>
> <td>Username:</td>
> <td><input type="text" name="uname" size="20"/></td>
> </tr>
> <tr>
> <td>Password:</td>
> <td><input type="password" name="pass" size="20"/></td>
> </tr>
> <tr>
> <td><input type="submit" value="Submit" name="login"/></td>
> </tr>
> </table>
>
> </form>
>
> </body>
> </html>
> <?php}
>
> ?>
>
> For some reason, the page doesn't get redirected to main.php. Can
> anyone 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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h51.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15667 seconds with 16 queries