/    Sign up×
Community /Pin to ProfileBookmark

Is redirect possible with PHP?

I am looking for a redirect script with PHP for two reasons:

[COLOR=red][b]one: [/b][/COLOR]I have a website that contains pages that should not be opened separately. I would like to force users to watch them only within the frameset, by redirecting them to my default page. With JavaScript it would look something like below, but I don’t want to use JavaScript.

[code]if (parent.frames.length==0)
window.location.replace(“http://www.alpinetrekking.com”)[/code]

[COLOR=red][b]two: [/b][/COLOR]Another usefull redirect script would be one depending on the users screen resolution. Any suggestions on that?

Cheers, Jochem ?

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@JonaDec 05.2003 — [font=arial]Hmm, I don't know if it's possible to check if there are frames in PHP. You could do it, if you could run PHP code after the page was loaded, and since it's server-side, you can't do that... You can't do a redirect depending on the screen resolution in PHP...[/font]

[code=php]
header("Location: page.php"); #redirect to page.php
[/code]


[b][J]ona[/b]
Copy linkTweet thisAlerts:
@jochemauthorDec 05.2003 — Thanks for your reply Jona

There doesn't need to be a check for frames I guess. All I need is a script that will automatically redirect to the default page. PHP is completely new to me, so would you please be so kind to write out the entire code for me?

If I use PHP coding, the extention of the page will be .php in stead of .htm, right? If so, can the rest of the page (besides the redirect script) be just an ordinairy HTML page, starting and ending with <html> </html>? Or are we talking something completely different here? Or is there a possibillity to call upon an external PHP file, such as with external CSS or JavaScript?

Cheers, Jochem ?
Copy linkTweet thisAlerts:
@JonaDec 05.2003 — [font=arial]Yes, a .php extension is necessary; however, the document type definition (DTD) is what defines the markup language used. Chances are, you're using HTML, so this should be the first line of all documents, including .htm or .html ones:[/font]

[font=monospace]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

[/font]

[font=arial]Your next line will be <html> and the last line will be </html>. In my signature there are links to things such as the DTD list and whatnot, which you may find useful for more information on what a DTD is.

Now then, wherever you run PHP code in the page, it is a tag like this:[/font]

[code=php]
<?
// PHP code
?>
[/code]


[font=arial]The two forward slashes indicate a single-line comment, just like JavaScript, but the pound sign (#) also does. I'd suggest taking the tutorial at www.php.net/tutorial for the basics. (That URL may be incorrect; if so, try looking around there, and be sure to use the manual as a reference.)[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@pyroDec 05.2003 — [i]Originally posted by jochem [/i]

[B]Or is there a possibillity to call upon an external PHP file, such as with external CSS or JavaScript?[/B][/QUOTE]

[URL=http://us4.php.net/manual/en/function.include.php]include[/URL] may be what you are looking for - depends what you need to do.
Copy linkTweet thisAlerts:
@jochemauthorDec 05.2003 — Funny, when I post your code using the code-tag, the script looks different from when I post it with the PHP-tag. See for yourself...&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html&gt;

&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;/head&gt;

<i> </i>&lt;?
<i> </i>header("Location: <a href="http://www.alpinetrekking/com">http://www.alpinetrekking/com</a>");
<i> </i>?&gt;

&lt;body&gt;
bla bla bla bla bla bla bla
&lt;/body&gt;

&lt;/html&gt;
[code=php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>Test</title>
</head>

<?
header("Location: http://www.alpinetrekking/com");
?>

<body>
bla bla bla bla bla bla bla
</body>

</html>[/code]
So, which of the above is correct? I cannot test it since to my knowledge PHP can only be tested online, and my current ISP does not support PHP.

It doesn't seem logical to put that script inside the <body> of the page, since the PHP should be the first thing read by the browser when a user tries to open the page. So could it be put behind the <head> or even higher?

Cheers, Jochem ?
Copy linkTweet thisAlerts:
@jochemauthorDec 05.2003 — Or can it be called from an external file?
Copy linkTweet thisAlerts:
@JonaDec 05.2003 — [font=arial]The first one is correct.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@jochemauthorDec 05.2003 — That's weird, the second one was generated automatically by this forum!!

Is the location between the <head> and <body> tags okay, or should it be elsewhere? And like I asked, can it be in an external file, so the page itsself will remain a .htm file in stead of .php?

Cheers, Jochem ?
Copy linkTweet thisAlerts:
@JonaDec 05.2003 — [font=arial]You can't include files with PHP code in a .htm page.

The code goes before anything in the HTML (before ANYTHING). in a .php document.[/font]

[b][J]ona[/b]
Copy linkTweet thisAlerts:
@jochemauthorDec 05.2003 — So that would result in something like this:&lt;?
header("Location: <a href="http://www.alpinetrekking/com">http://www.alpinetrekking/com</a>");
?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html&gt;

the rest
In another forum I came accross this trick to insert php into a .htm file.&lt;img src="http://www.server_with_php.com/the_file.php" height="1" width="1"&gt;Would that do the job?

Pyro, include() does not seem to be what I am looking for.

Cheers, Jochem ?
Copy linkTweet thisAlerts:
@Jeff_MottDec 05.2003 — Just to clarify a couple things...It doesn't seem logical to put that script inside the <body> of the page, since the PHP should be the first thing read by the browser when a user tries to open the page[/quote]The browser does not read PHP at all. It is a server-side language. This means that all PHP scripting is read and processed at the server. All the browser sees is the output.

Normally it would be fine to put PHP scripts anywhere within a page, but [font=courier]header()[/font] is a special case, since HTTP headers must appears before any content.That's weird, the second one was generated automatically by this forum!![/quote]Altering code for presentation on a Web site (what the forum generated) is certainly not what you're trying to do.In another forum I came accross this trick to insert php into a .htm file.[/quote]This depends wholly on what the PHP code needs to do. If it is working with external files (such as perhaps incrementing a counter) then it would work fine, but if you need to print something to the page (in our case headers) then it will not work.Pyro, include() does not seem to be what I am looking for.[/quote]At the time you had asked how to include PHP code, and [font=courier]include()[/font] is the answer to that question. If it is not what you are looking for then you need to be more precise in your posts about what you are looking for.
Copy linkTweet thisAlerts:
@jochemauthorDec 06.2003 — Thanks for your replies. I guess I have no choice but to rename the .htm to .php and place the code all the way at the beginning.

Cheers, Jochem ?
Copy linkTweet thisAlerts:
@pyroDec 06.2003 — [i]Originally posted by jochem [/i]

[B]I guess I have no choice but to rename the .htm to .php and place the code all the way at the beginning.[/B][/QUOTE]
If having to rename your files really matters to you, and you are on an Apache server, you can use a .htaccess file to tell it to process .htm files as PHP.
Copy linkTweet thisAlerts:
@jochemauthorDec 08.2003 — Thanks Pyro, good to know. I'll keep that in mind.

Cheers, Jochem ?
×

Success!

Help @jochem spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...