You are here: Re: General PHP applications « PHP Programming Language « IT news, forums, messages
Re: General PHP applications

Posted by David Haynes on 03/23/06 16:11

odwrotnie wrote:
> On Wed, 22 Mar 2006 23:02:36 +0100, David Haynes
> <david.haynes2@sympatico.ca> wrote:
>
>> The rule is foo.php is only called by foo.ctrl.php (via a header()
>> redirect) and foo.php will only call foo.ctrl.php (via POST or GET).
>
> Can You give me examples about "via a header()" and "redirect" and POST
> and GET. I dont really understand what do You mean (I am new to php)...
>
> --Best regards,
> Odwrotnie.
Sure.

POST and GET refer to the way data may be passed from an HTML <form> to
a program - in this case a php program. So, when you create a form, you
have something like:

<form action="foo.ctrl.php" method="POST">
<input type="text" name="something">
<input type="submit">
</form>

Clicking on the OK button (of the submit) will cause the data in the
'text' variable to be sent to the program foo.ctrl.php.

The foo.ctrl.php program can access the 'something' data as
$_POST['something'].

If you set method="GET" or use a url like
http://foo.ctrl.php?something="hey there", then the program can access
the 'something' data as $_GET['something'].

Let's say that foo.ctrl.php wants to display the value of 'something' in
a web page. My way would be to have foo.ctrl.php redirect the http
session to the display page using a header() call.

For example:

<?php
header("Location: show_text.php?something=$_POST['something']");
?>

The page show_text.php can get the text data using the $_GET['something'].
For example:

<html>
....
<body>
The text you entered is <?php echo $_GET['something'];?><br/>
</body>
</html>

Now, sending a lot of data to a form via the URL string can get very
messy. PHP provides another method - sessions - to pass data quietly.

Using sessions, php.ctrl.php would become:

<?php
session_start();
$_SESSION['something'] = $_POST['something'];
session_write_close();
header("Location: show_text.php");
?>

and show_text.php would become:
<?php
start_session();
?>
<html>
....
<body>
The text you entered is <?php echo $_SESSION['something'];?><br/>
</body>
</html>

The location command has its own issues. Someone posted a nice routine
(I'm sorry but I can't recall who it was) to enhance the location
command. My version of this is:

/**
* Module: redirect.inc
*
* Manages a redirection when there may be a session active.
* Also correctly re-addresses URLs that are not absolute.
*
* @param string $url
*/
function redirect($url) {
if( substr($url, 0, 4) != 'http' ) {
if( isset($_SERVER['HTTP_HOST']) ) {
$url = 'http://'.$_SERVER['HTTP_HOST'].$url;
} else {
$url = 'http://localhost'.$url;
}
}
session_write_close();
header("location: $url");
exit;
}

This function makes all url addresses absolute and ensures that the
session is committed prior to the header() redirect.
(Thanks again to the original poster)

This should get you started.

-david-

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация