Page 1 of 1
redirecting to next page
Posted: Tue Aug 11, 2009 2:25 am
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";
}
?>
Re: redirecting to next page
Posted: Tue Aug 11, 2009 3:11 am
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

Re: redirecting to next page
Posted: Tue Aug 11, 2009 3:17 am
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
Re: redirecting to next page
Posted: Tue Aug 11, 2009 3:19 am
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";
}
?>
Re: redirecting to next page
Posted: Tue Aug 11, 2009 3:19 am
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.
Re: redirecting to next page
Posted: Tue Aug 11, 2009 3:52 am
by sathyan
Remove All echo statements used before the header() redirect.......and try again..........
Re: redirecting to next page
Posted: Tue Aug 11, 2009 4:22 am
by yasir_memon
thx for ur replies my this problem is resolved
Re: redirecting to next page
Posted: Tue Aug 11, 2009 4:25 am
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
Re: redirecting to next page
Posted: Tue Aug 11, 2009 4:42 am
by onion2k
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).

Re: redirecting to next page
Posted: Tue Aug 11, 2009 4:43 am
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!