Redirect in PHP

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

Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Redirect in PHP

Post by Dark_AngeL »

Hi

I have a question in ASP we have Response.Redirect" ";
What do we use in PHP?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post 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
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post by Dark_AngeL »

Code: Select all

$url = "http://localhost/Warehouse_Services_Operations_System/main.php"
Will this do?!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The "bug" is using single quotes. Single quote strings do not parse variables contained in them.

Read strings for more information.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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.
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post 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
Last edited by Dark_AngeL on Tue Feb 28, 2006 12:59 am, edited 2 times in total.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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().
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post 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:
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Read Jasons tutorial about that.

:arrow: here


Good Luck!
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post by Dark_AngeL »

Thank you!
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post 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>
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
Dark_AngeL
Forum Commoner
Posts: 39
Joined: Tue Feb 21, 2006 5:16 am

Post by Dark_AngeL »

I did that :?
but still no luck
Post Reply