Php Flow » Php

PHP : How To Use PHP_SELF

PHP_SELF is global variable of PHP language. This function returns the name and path of the current file with root folder.Normally this variable are used in FORM tag to submit current script on current page.There are also have some exploits.

Example:
1- Suppose your php file is located at the address:
http://localhost /action.php
In this case result will be
“/action.php”

2- Suppose your php file address has some variable then:

http://localhost/app/view/AppList.php?site=test&page=1

For this URL, PHP_SELF will be :
“/app/view/AppList.php?site= test&page=1”

How to use PHP_SELF on Action field of the form

Normally we are use PHP_SELF variable is in the action field of the “form” tag. The action field of form tells interpreter where file will submit and handle this form.

1
<form method="post" style="width:300px;float:right" action="<?php echo $_SERVER['PHP_SELF'] . '?site= ' . $_REQUEST['site']. '&page=' . $page;?>">
Complete code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
if(isset($_POST[‘search’]))
{
    echo form submitted";
}
?>

<div class="well form-search">
<form method="post" style="width:300px;float:right" action="<?php echo $_SERVER['PHP_SELF'] . '?site= ' . $_REQUEST['site']. '&page=' . $page;?>">
<input type="text" name="search_key" value="<?php echo $applicationObj->GetKeyword()?>" id="search_key" class="input-medium search-query">
<button type="submit" class="btn" id="search" value="search" name ="search">Search</button></form>
</div>

PHP_SELF Issue:
If user enter the url with action file name then form will submit.
Example:

http://localhost/app/view/AppList.php?search=true

Another way hacker can also execute javascript code.

To avoid this you will use htmlentities PHP function

1
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>