Page 1 of 3

Redirect in PHP

Posted: Mon Feb 27, 2006 11:42 pm
by Dark_AngeL
Hi

I have a question in ASP we have Response.Redirect" ";
What do we use in PHP?

Posted: Mon Feb 27, 2006 11:51 pm
by feyd

Posted: Tue Feb 28, 2006 12:35 am
by Dark_AngeL
This is what i did

Code: Select all

$url = "main.php";

if ($name == "Heba" &&  $pass == "123")
	{
	header('Location: $url'); /* Redirect browser */
			
	exit;				
	?>
            <?
	  }
	else
	{
	echo ("Sorry. You are not authorized to access this page");
	echo ("<br>"."Go <a href='Index.php'> Back </a>");
	 } ?>


If the name is Heba and pass is 123 i want to direct the user 2 main.php

Posted: Tue Feb 28, 2006 12:38 am
by nickman013
You need to specify the full URL for $url.

For example

videos.php would be somthing like http://www.mysite.com/videos.php.

Posted: Tue Feb 28, 2006 12:40 am
by Dark_AngeL

Code: Select all

$url = "http://localhost/Warehouse_Services_Operations_System/main.php"
Will this do?!

Posted: Tue Feb 28, 2006 12:44 am
by feyd
The "bug" is using single quotes. Single quote strings do not parse variables contained in them.

Read strings for more information.

Posted: Tue Feb 28, 2006 12:45 am
by nickman013

Code: Select all

<?
$url = "main.php";
if ($name == "nicky" &&  $pass == "123") 
   { 
   header('Location:http://localhost/Warehouse_Services_Operations_System/'.$url); /* Redirect browser */ 
          
   exit;             
     } 
   else 
   { 
   echo ("Sorry. You are not authorized to access this page"); 
   echo ("<br>"."Go <a href='Index.php'> Back </a>"); 
    } ?>
Edit
or as feyd said you can do it like this.

Code: Select all

<?
$url = "http://localhost/Warehouse_Services_Operations_System/main.php";
if ($name == "nicky" &&  $pass == "123") 
   { 
   header("Location:$url"); /* Redirect browser */ 
          
   exit;             
     } 
   else 
   { 
   echo ("Sorry. You are not authorized to access this page"); 
   echo ("<br>"."Go <a href='Index.php'> Back </a>"); 
    } ?>
That will work, but if you are doing stuff like this, I suggest you use a database, and sessions. If you need help, I will be glad to help you, as so would a lot of people on this board will. :D

Ps. Localhost, will only work on your machine.

Posted: Tue Feb 28, 2006 12:54 am
by Dark_AngeL
Oh thank you so much all

nickman013 I will be using a DB soon and i sure will need ur help
i am starting it easy now

Posted: Tue Feb 28, 2006 12:57 am
by nickman013
No problem.

Database stuff like this is very simple. You just need to make a table and 2 fields. The rest is simple coding just like the one you made above. Also it is much more secure. But if you want to make your script you got now a lot more secure, use md5().

Posted: Tue Feb 28, 2006 1:00 am
by Dark_AngeL
Hopefully i will learn


What does this error mean

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\kdb3684\Desktop\Warehouse_Services_Operations_System\check_login.php:7) :roll:

Posted: Tue Feb 28, 2006 1:03 am
by nickman013
Read Jasons tutorial about that.

:arrow: here


Good Luck!

Posted: Tue Feb 28, 2006 1:29 am
by Dark_AngeL
Thank you!

Posted: Tue Feb 28, 2006 3:33 am
by Dark_AngeL
here is what i did but still it doesnt work
it gives me an empty page with no message:(

Code: Select all

<?php 
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit') //if form has been submitted
{
    $url = "main.php";
    $denied = "";
    $name = $_POST['name'];
    $pass = $_POST['pass'];
    if ($name == "Heba" &&  $pass == "123")
    {
        header('Location: '.$url.''); /* Redirect browser */
        exit;                
    }
    else {
        $denied = "Sorry. You are not authorized to access this page<br>Go <a href='index.php' onclick='history.go(-1);return false'> Back </a>";
    }
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>TITLE</title>
<body>
<?php
if(!empty($denied))
{
echo $denied;
}
?>
</body>
</html>

Posted: Tue Feb 28, 2006 3:53 am
by AKA Panama Jack
The url used in the header must be a FULL url. This means your $url="main.php" needs to be $url="http://www.yourwebside.com/main.php" before it can work.

Posted: Tue Feb 28, 2006 4:08 am
by Dark_AngeL
I did that :?
but still no luck