redirecting to next page

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

Post Reply
yasir_memon
Forum Commoner
Posts: 48
Joined: Tue Aug 11, 2009 12:29 am

redirecting to next page

Post by yasir_memon »

hi guys i want to redirect the page when i gaive correct user name and password kindly tell me how can i do this
<?php
$A=$_POST["txt1"];
$B=$_POST["txt2"];
$conn=mysql_connect('localhost','root','server') or die("Error connection server");
$db=mysql_select_db("login",$conn) or die("error connecting database");
echo "connection mysql<br/>";

$sql="select name from log where name='$A' and pwd='$B' ";
echo "connection mysql<br/>";
$res=mysql_query($sql) or die(mysql_error());
echo "connection mysql<br/>";
$count = mysql_num_rows( $res);

if($count>0)
{
// redirect to "index.php" page
}
else
{
echo "invalid user name or password";
}
?>
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: redirecting to next page

Post by juma929 »

Hello,

Providing your not outputting anything prior to using this, you could use a header redirect.

Code: Select all

 
 
header('Location: http://www.me.com/');
 
 
Thanks :D
yasir_memon
Forum Commoner
Posts: 48
Joined: Tue Aug 11, 2009 12:29 am

Re: redirecting to next page

Post by yasir_memon »

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\log.php:6) in C:\wamp\www\log.php on line 16

when i am using header this message is occuring and page is not redirecting kindly tell me how can i do this
yasir_memon
Forum Commoner
Posts: 48
Joined: Tue Aug 11, 2009 12:29 am

Re: redirecting to next page

Post by yasir_memon »

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\log.php:6) in C:\wamp\www\log.php on line 16

when i am using header this message is occuring and page is not redirecting kindly tell me how can i do this


<?php
$A=$_POST["txt1"];
$B=$_POST["txt2"];
$conn=mysql_connect('localhost','root','server') or die("Error connection server");
$db=mysql_select_db("login",$conn) or die("error connecting database");
echo "connection mysql<br/>";

$sql="select name from log where name='$A' and pwd='$B' ";
echo "connection mysql<br/>";
$res=mysql_query($sql) or die(mysql_error());
echo "connection mysql<br/>";
$count = mysql_num_rows( $res);

if($count>0)
{
header("Location: http://index.php");
}
else
{
echo "invalid user name or password";
}
?>
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: redirecting to next page

Post by onion2k »

He said "Providing your not outputting anything prior to using this". You obviously are. You need to reorganise your code so that the header() bit is done before anything else is sent to the user. Try deleting all the echo statements.
sathyan
Forum Newbie
Posts: 9
Joined: Thu Aug 06, 2009 12:35 am

Re: redirecting to next page

Post by sathyan »

Remove All echo statements used before the header() redirect.......and try again..........
yasir_memon
Forum Commoner
Posts: 48
Joined: Tue Aug 11, 2009 12:29 am

Re: redirecting to next page

Post by yasir_memon »

thx for ur replies my this problem is resolved
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: redirecting to next page

Post by turbolemon »

You could use output buffering to capture all output before the header, then output errors if you don't redirect. http://us2.php.net/manual/en/ref.outcontrol.php
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: redirecting to next page

Post by onion2k »

turbolemon wrote:You could use output buffering to capture all output before the header, then output errors if you don't redirect. http://us2.php.net/manual/en/ref.outcontrol.php
That's a pretty nasty performance overhead that you can avoid just by organising your code better. I wouldn't recommend it.

In other news.. I'm following you on Twitter now (@onion2k). :)
towerspl
Forum Newbie
Posts: 20
Joined: Mon Aug 03, 2009 7:37 am

Re: redirecting to next page

Post by towerspl »

Dont forget to put exit() after your header() as the script will continue to run after your header() which can cause some headaches!
Post Reply