Need help with $_SERVER['PHP_SELF']

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
akphidelt
Forum Newbie
Posts: 6
Joined: Wed Feb 13, 2008 2:09 pm

Need help with $_SERVER['PHP_SELF']

Post 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!
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

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

Post by yacahuma »

you can also use action="#" I am not sure if there is anything bad about doing this.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post 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.
Post Reply