What is wrong?

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
cmarc
Forum Newbie
Posts: 4
Joined: Sat Mar 01, 2008 12:03 am

What is wrong?

Post by cmarc »

The frustrations are piling up for an absolute newbie...

I can't even get simple things to work like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>justatitle.dk</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>

</head>

<body>

<?

header ("Location: http://www.justawebsite.com/folder/main.php");
exit;

?>

</body>
</html>

Isn't this supposed to automacially send me on to http://www.justawebsite.com/folder/main.php when I visit a page with the code above?!

What is wrong?

Thank you.

- cmarc
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: What is wrong?

Post by Sekka »

Headers can only be sent before anything what so ever is sent to the user.

If you are wanting to redirect someone, all you need to do is,

Code: Select all

<?php
 
header ("Location: http://www.justawebsite.com/folder/main.php");
exit();
 
?>
Hope this helps!
cmarc
Forum Newbie
Posts: 4
Joined: Sat Mar 01, 2008 12:03 am

Re: What is wrong?

Post by cmarc »

Okay, maybe that's my problem then.

Is there a way to redirect in pure html-code then?

Thank you.

- cmarc
cmarc
Forum Newbie
Posts: 4
Joined: Sat Mar 01, 2008 12:03 am

Re: What is wrong?

Post by cmarc »

I'm now able to redirect using PHP just by making sure that nothing else comes first, then it works.

But this is my exact problem as I wanted to redirect from a page that specifies assorted variables to pass their values on to the main page, where they will be used only once.

Is there a way to do this?

- cmarc
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: What is wrong?

Post by devendra-m »

Is there a way to redirect in pure html-code then?

Code: Select all

<meta http-equiv="refresh" content="0;URL=test.php">
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: What is wrong?

Post by Sekka »

cmarc wrote:I'm now able to redirect using PHP just by making sure that nothing else comes first, then it works.

But this is my exact problem as I wanted to redirect from a page that specifies assorted variables to pass their values on to the main page, where they will be used only once.

Is there a way to do this?

- cmarc
You can pass variables from page to page in a number of ways. The two most popular are,

Sessions

Page 1:

Code: Select all

session_start();
$_SESSION['var1'] = "something";
 
header ("Location: http://www.somewhere.com/somepage.php");
exit ();
Page 2:

Code: Select all

session_start();
if (isset ($_SESSION['var1'])) {
    $var1 = $_SESSION['var1'];
    // Do something
}
URL Query Strings

Place the variables on the end of the URL link to the next page, e.g.

Page 1:

Code: Select all

header ("Location: http://www.somewhere.com/somepage.php?var1=something");
exit ();
Page 2:

Code: Select all

if (isset ($_GET['var1'])) {
    $var1 = $_GET['var1'];
    // Do something
}
devendra-m wrote:

Code: Select all

<meta http-equiv="refresh" content="0;URL=test.php">
That method doesn't always work and is disliked by many search engines. You can get black listed for using them. I would recommend redirecting in PHP instead.
Post Reply