php $_SERVER['PHP_SELF'] to include query string
You could use:
$_SERVER['REQUEST_URI']
to get everything after the domain name folders, query strings etc.
You could then either use the other $_SERVER variables to add the domain, or it should be fine to use without a domain name provided you're not changing hosts.
PHP_SELF not working if URL has $_get value?
Update 1:
I want that when I submit the form i should return to same url but it
is not
You can simply keep the action attribute empty in case you want the form to return to the same page.
<form method="post" action="">
Update 2:
While it's working, according to the W3C documentation it's not a valid value.
The action and formaction content attributes, if specified, must have
a value that is a valid non-empty URL potentially surrounded by
spaces.
Please notice, PHP_SELF doesn't return the query string (?x=y&z=d).Just the dir and the file name.
PHP_SELF
The filename of the currently executing script, relative to the
document root. For instance, $_SERVER['PHP_SELF'] in a script at the
address http://example.com/foo/bar.php would be /foo/bar.php. The
FILE constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor
this variable contains the script name since PHP 4.3.0. Previously it
was not available.
How to work with $_SERVER['QUERY_STRING']
Instead of reusing QUERY_STRING
, you should assemble it anew with http_build_query()
.
// Merge $_GET with new parameter
$QS = http_build_query(array_merge($_GET, array("page"=>2)));
// You should apply htmlspecialchars() on the path prior outputting:
echo "<a href='" . htmlspecialchars("$_SERVER[PHP_SELF]?$QS") . "'> $i </a>";
Thus you have all current $_GET
parameters included, but can add or replace entries with new values. And it's ensured that each appears only once.
PHP add/modify a query string param & get the URL
Just to make sure you know about it, use parse_str
to parse your query string:
<?php
parse_str($_SERVER['QUERY_STRING'], $query_string);
$query_string['page'] = basename($_SERVER['PHP_SELF']);
$rdr_str = http_build_query($query_string);
Execute $_SERVER['PHP_SELF'] without removing my $_GET variables
$_SERVER['REQUEST_URI']
: The URI which was given in order to access this page.
Reference
Your form should be like:
<form action="<?php echo $_SERVER['REQUEST_URI'] ?>">
your inputs
</form>
$_Server[php_self] i need to pass multiple variables
You can pass multiple parameters by separating them with &
. You can also use http_build_query()
to construct the URL query string from an associative array.
$params = array('data1' => $data1, 'data2' => $data2, 'cod' => $cod);
$params['startrow'] = $prev >= 0 ? $prev : ($startrow + 30);
$query = http_build_query($params);
echo '<a class="da" href="' . $_SERVER['PHP_SELF'] . '?' . $query . '">Previous</a>';
Add, change, and remove GET parameters from query string
function changeQueryString($queryStr, $updateStr, $removeStr) {
parse_str($queryStr, $queryArr);
parse_str($updateStr, $updateArr);
parse_str($removeStr, $removeArr);
$changedArr = array_merge($queryArr, $updateArr);
foreach($removeArr as $k => $v) {
if(array_key_exists($k, $changedArr)) {
unset($changedArr[$k]);
}
}
return http_build_query($changedArr);
}
$str = 'apple=green&banana=yellow2&navi=blue&clouds=white&car=black';
$changedQuery = changeQueryString($str, 'eyes=2&fingers=10&car=purple', 'clouds&apple');
var_dump($changedQuery);
This should work for you, utilizing parse_str(), array_merge() and http_build_query()
PHP How to get the url of the page where include is used
Is there a specific reason why you want to use file_get_contents? You could use include_once('header.php');
. in your index.php file. Determining if you are on index.php page you can use in your header.php file
if(basename($_SERVER['PHP_SELF']) == "index.php") {
/* show whatever you need */
}
PHP_SELF not working as expected
$_SERVER["REQUEST_URI"]
will have what you want.
Related Topics
How to Add Rel="Nofollow" to Links with Preg_Replace()
Url Encoded Forward Slashes Breaking My Codeigniter App
Where to Use MySQL_Real_Escape_String to Prevent SQL Injection
Http Header for Downloading Microsoft Word and Excel Files
Pdo Bind Unknown Number of Parameters
How to Decode a Base64 String (Gif) into Image in PHP/Html
Moving from MySQL to MySQLi or Pdo
Echo a Multi Dimensional Array
Import Class Conditionally with the Keyword 'Use'
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce