[thelist] Form action = $PHP_SELF

Rodrigo Fonseca lists at vega.eti.br
Thu Oct 16 00:41:09 CDT 2003


Joshua Hmielowski wrote:
> does anyone know the best way to action a form to itself for all browsers?
> 
> originally I had    <form action= "<? thisphpdocument.php ?> ">
> 
> but I come to find out it does not work on all browsers. specifically 
> netscape or older versions of explorer.
> I also tried the $PHP_SELF variable with the same outcome.
> 
> that outcome is that it actions to the index page.

I didn't understand what the problem is, are you having problem to write
the page name to the form action or the relative page (without the
server name included) does not work on Netscape or older IEs?

Anyway, if the problem is not being able to "write" the page URL, the
solution is "almost" what you've tried before:

$PHP_SELF alone only works if "register_globals" is set by PHP but
most hosts have it disabled for security reasons, so you should try
$_SERVER['PHP_SELF'] instead.

If PHP is set to work with short_open_tag = On
Use <?=$_SERVER['PHP_SELF']?>
or <? print $_SERVER['PHP_SELF']; ?>

If short_open_tag is set to "Off" the solution is:
<?php print  $_SERVER['PHP_SELF']; ?>

And...
If the problem with the old browsers is the relative URL (although I
can't remember having trouble with that even in Netscape 3), you should
concatenate the $HTTP_HOST to the page path:

printf("http://%s%s",$_SERVER['HTTP_HOST'],$_SERVER['PHP_SELF']);

This will output http://www.yourdomain.com/your/page/path.php

Do not use HTTP_NAME or SERVER_NAME because they may be set to
machinename.yourdomain.com or something else.

There is a third option here: check if there is a tag "base href" in
your documents.
If there is a <base href="http://www.yourdomain.com" /> and your page
is in http://www.yourdomain.com/forms/theform.php your action should
be "/forms/theform.php" and NOT "theform.php" or the client will be
redirected to http://www.yourdomain.com/theform.php (and probably this
page does not exist).

Most server admins like to redir the error 404 pages to the site root,
and maybe this is what's happening in your case (at least I think it's
the most likely situation).

HTH,

	Rodrigo Fonseca.



More information about the thelist mailing list