How to redirect from a php page to a HTML page?

Problems with the Windows version of XAMPP, questions, comments, and anything related.

How to redirect from a php page to a HTML page?

Postby wasabihowdi » 09. March 2008 22:30

Hey guys,

I would like to know how to redirect from a php page (A.php) to a HTML page <B.html)?

I've done some string processing and stored in a variable in A.php and would like to redirect to B.html and display the result of the string process.

Is it possible?

Thanks!

Howdi
wasabihowdi
 
Posts: 11
Joined: 24. January 2008 06:23

Postby Xardas der Dunkle » 09. March 2008 22:37

It is possible, but why you don't display them on A.php?

The A.php:
Code: Select all
<?php
  $var = 'Hello World';

  header('Location: B.php?var=' . urlencode($var));
?>


The B.php:
Code: Select all
<?php
 echo $_GET['var'];
?>


bye
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby sari42 » 09. March 2008 22:38

you can only move data to a .htm(l) page by means of GET (uri), POST (form) or COOKIE variables and displaying them using javascript


IMHO - I may be wrong though

p.s.
if you use a.php to create b.html you can use
<meta http-equiv="refresh" content="1; URL=b.html">
at the end of a.php
Last edited by sari42 on 09. March 2008 22:52, edited 1 time in total.
sari42
 
Posts: 800
Joined: 27. November 2005 18:28

Postby Xardas der Dunkle » 09. March 2008 22:52

A refresh with the <meta>-Tag is not the best way, I think.
If you use the Apache it is mutch better to use the Location-Header for redirect.
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby sari42 » 09. March 2008 22:54

Xardas der Dunkle wrote:A refresh with the <meta>-Tag is not the best way, I think.
If you use the Apache it is mutch better to use the Location-Header for redirect.

ok,
but then you can't output anything on a.php or have to use buffering (ob_start...)

but the effect is the same, no?
sari42
 
Posts: 800
Joined: 27. November 2005 18:28

Postby Xardas der Dunkle » 09. March 2008 22:58

The problem with the meta-refresh is that, it not work always and it not redirect directly like the location-header.

The problem with the location-header is, that it not work with other webservers like the iis :?.
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby Nobbie » 09. March 2008 23:03

Why not simply include the file?

Code: Select all
<?php
...
    include('B.html');
...
?>


This results in an output of B.html and all variables of A.php are available in B.html - nothing is easier than that!

Regards
Nobbie
Nobbie
 
Posts: 8775
Joined: 09. March 2008 13:04

Postby sari42 » 09. March 2008 23:04

Xardas der Dunkle wrote:The problem with the meta-refresh is that, it not work always and it not redirect directly like the location-header.

The problem with the location-header is, that it not work with other webservers like the iis :?.


well,
but the OP asked for b.html, not b.php

and <meta ... works as well as location... - you can even provide both ;)

any info when "meta redirect" doesn't work, but "header('location..." does?
Last edited by sari42 on 09. March 2008 23:06, edited 2 times in total.
sari42
 
Posts: 800
Joined: 27. November 2005 18:28

Postby Xardas der Dunkle » 09. March 2008 23:05

@Nobbie
In a html-file are php-variables not available ... except you have tell the Webserver that it have to parse html-files, by the php-interpreter, too.
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby sari42 » 09. March 2008 23:09

Nobbie wrote:Why not simply include the file?

you can't "include" html files
_______

correction:
php can include any (text) file,
if the redirect is not needed nobbie's solution is the most elegant ;)
_______

doesn't answer the question of the OP
Last edited by sari42 on 10. March 2008 11:04, edited 1 time in total.
sari42
 
Posts: 800
Joined: 27. November 2005 18:28

Postby Xardas der Dunkle » 09. March 2008 23:23

sari42 wrote:
Nobbie wrote:Why not simply include the file?

you can't "include" html files
you only can use file_get_contents('....html'), but that doesn't answer the question of the OP


You can, but they will not be parsed.
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Postby Nobbie » 10. March 2008 00:14

>you can't "include" html files

What?? Of course you can.

And they will be parsed (Of course again!).

@Xardas
>In a html-file are php-variables not available ... except you have tell the Webserver that it have to parse html-files, by the php-interpreter, too.

No, you are wrong. You have to tell a webserver to call PHP if you enter HTML file name via URL. But if PHP is already active, ALL(!) files are parsed which are included. Always. Does not matter which extensions these have. Included files are parsed!
Nobbie
 
Posts: 8775
Joined: 09. March 2008 13:04

Postby sari42 » 10. March 2008 10:23

>Included files are parsed!
well,
but html files usually don't have <?php tags included
(sorry if I didn't expressed correctly what I wanted to say..),
otherwise I don't see a reason not to give them the .php extension.
sari42
 
Posts: 800
Joined: 27. November 2005 18:28

Postby Nobbie » 10. March 2008 10:41

>but html files usually don't have <?php tags included
(sorry if I didn't expressed correctly what I wanted to say..),

But it is exactly that, what "wasabihowdi" did and what is looking for:

I've done some string processing and stored in a variable in A.php and would like to redirect to B.html and display the result of the string process.


And for this scenario the include() is the perfect solution.

By the way: I also include PHP-Tags into HTML files and use these as templates. As PHP already is a perfect and powerfull template engine and there is no need to use crap things like "Smarty" (what I call a "Template Template Engine Engine"). By simply including() the Templates into PHP all variables are expanded.

For that reason, I use to put all procedural and functional coding into *.php files, and all my templates are stored in *.html.
Nobbie
 
Posts: 8775
Joined: 09. March 2008 13:04

Postby sari42 » 10. March 2008 10:59

ok for templating,
but the OP asked explicitly for a "redirect" to a new page, maybe he needs to have "b.html" appearing in the browser location line?

>there is no need to use crap things like "Smarty"
full ack ;)
sari42
 
Posts: 800
Joined: 27. November 2005 18:28

Next

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 76 guests