Page 1 of 1

Need help with $_SERVER['PHP_SELF']

Posted: Mon Feb 25, 2008 11:52 am
by akphidelt
Hey there, I'm having a problem getting $_SERVER['PHP_SELF'] to work properly. Im trying to attach it to a form... so I have it like this

Code: Select all

 
$server = $_SERVER['PHP_SELF'];
 
<FORMS METHOD="POST" ACTION="$server">
 
But when I click on the submit button for the form it gives me an error and the URL looks like this

http://localhost/.'/currentpage.php

I can't for the life of me find a solution on the internet and was wondering if you guys had a clue on what was going on!

Re: Need help with $_SERVER['PHP_SELF']

Posted: Mon Feb 25, 2008 12:26 pm
by yacahuma
you can also use action="#" I am not sure if there is anything bad about doing this.

Re: Need help with $_SERVER['PHP_SELF']

Posted: Mon Feb 25, 2008 12:41 pm
by califdon
echo the value of $server to see what it contains. You will probably see immediately what needs to be done to form a valid URL.

Re: Need help with $_SERVER['PHP_SELF']

Posted: Mon Feb 25, 2008 12:43 pm
by Luke
Well first of all you can't just output html directly in php. You have to "exit" out of php first...

Code: Select all

<?php
$server = $_SERVER['PHP_SELF'];
?>
<FORMS METHOD="POST" ACTION="<?php echo $server; ?>">
Second, don't use PHP_SELF.

Re: Need help with $_SERVER['PHP_SELF']

Posted: Mon Feb 25, 2008 2:45 pm
by RobertGonzalez
Try something like this:

Code: Select all

<?php
$action = basename(__FILE__);
?>
<FORMS METHOD="POST" ACTION="<?php echo $action; ?>">
PHP_SELF is a very insecure way of capturing the current page name. Ask Ninja. He wrote an article on it.