Can I remove the query string from PHP after the page loads? - php

I have a URL like :http://localhost/foldername/page?name=xyz after page loads.
The page contents is coming by fetching the data from database with name=xyz.
But I want to the URL like : http://localhost/foldername/page/xyz after the page loads. Is there any way ?

create a .htaccess file inside foldername and put this code
RewriteEngine On
RewriteRule ^page/([a-zA-Z0-9-/]+)/?$ index.php?name=$1 [L]
Access it from browser
http://localhost/foldername/page/xyz
now you can access name in index.php with get request $_GET['name'];

Try this, get url and use strtok for clear get value in url
if (isset($_GET['name'])) {
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = strtok($url, '?');
$get_name = $_GET['name'];
$url = "Location: ".$url."/".$get_name;
header($url);
}

Related

PHP Replace basename in the URL

I have this function.
function getCallbackUrl(){
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . 'response.php';
}
On my URL http://localhost/gateways/payu/index.php the above function displays URL like this http://localhost/gateways/payu/index.phpresponse.php. No idea why it is happening. The function seems correct to me. Maybe I am missing out something that I am not able to replace the base name from index.php to response.php. Any help would be truely appreciated. Thank you :)
Currently, your $_SERVER['REQUEST_URI'] itself has index.php, hence you are facing this issue, where response.php is concatenated instead of replacing. A quick fix is as below:
$_SERVER['REQUEST_URI'] = str_replace(basename($_SERVER['REQUEST_URI']),'response.php',$_SERVER['REQUEST_URI']);
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
You can also use a combination of parse_url(),str_replace() and basename() to achieve this.
Parse the url and get the URI path.
Get the basename of the URI.
Replace it with the one you want to.
Join these pieces together and return the URL.
Snippet:
<?php
function getCallbackUrl($url,$replacement_file){
$url_data = parse_url($url);
$url_data['path'] = str_replace(basename($url_data['path']),$replacement_file,$url_data['path']);
$url = $url_data['scheme'] . "://" . $url_data['host'] . $url_data['path'];
if(!empty($url_data['query'])) $url .= '?' . $url_data['query'];
return $url;
}
echo getCallbackUrl('http://localhost/gateways/payu/index.php','response.php');

I want to redirect website visitor from "expl.com/... "to "expl.com/index.php/..."

This is my domain http://cdtr.cf it lands on index.php
Where I wrote some code which pulls the URL from URL box and trim its suffix to identify visitor's identity.
The Problem is when someone opening http://cdtr.cf/.... it shows error 404 page not found
while just http://cdtr.cd/ is working fine. All I need is When someone visit with http://cdtr.cf/.... they must be redirected to index.php and their request should be processed like http://cdtr.cf/index.php/.....
I want to keep the suffix anyhow for some purpose :-http://cdtr.cf/suffix.
It will be great if all this happen without any visible change in URL box.
Thanks in advance.
$link= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=== 'on'? "https" : "http") . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $link; //It is working fine on localhost but not when i put it live
This code will give you the rest of the url as per your requirement
<?php
$link = $_SERVER['REQUEST_URI'];
echo $link = ltrim($link, '/');
//if you want you can trim last / too using $link = rtrim($link, '/');
?>
If you want to get middle stuff like this yoursite.com/stuffs/removedthis
then you have to use below code
<?php
$link = $_SERVER['HTTP_HOST'];
$link .= $_SERVER['REQUEST_URI'];
$link = explode('/', $link);
echo $a = $link[1];
?>
Second example:
$link = "example.com/stuff/removethis/....blah";
$link = explode('/', $link);
echo $a = $link[1];

How to make sure a parameter "lang" always is present in url without adding it to all links?

I have a simple multi language website. The langauge of the displayed page is controlled by the use of a session variable, but I want users to be able to copy the url and send it to other people and end up on the same language page -- that is I want the "lang" url parameter to be present in the url always.
I could of course edit all links on the page and add it to them, but isn't there an easier way to do this? Is there an alternative solution?
Maybe you can try this:
<?php
//get full url
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//check if get lang exists.
if(isset($_GET['lang'])){
if($_GET['lang'] == "en"){
//then do nothing.
} else{
//get all parameters.
$query_arr = $_GET;
//chang lang parameter.
$query_arr["lang"] = "en";
$query = http_build_query($query_arr);
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
//make first part of url.
$first_url = 'http://' . $_SERVER['HTTP_HOST'] . $uri_parts[0];
//redirect to correct url.
header("location: " . $first_url . "?" . $query);
}
}else{
//redirect to correct url.
header("location: " . $url . "&lang=en");
}
?>
Hope this is wat you meant.
You can use something like:
<?php
session_start();
if(isset($_SESSION['lang'])){
$sessionLang = $_SESSION['lang'];
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$rUri = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(!(isset($_GET['lang']))){
if (strpos($rUri, '?')) { // returns false if '?' isn't there
$newUrl = "$rUri&$sessionLang";
header("Location: $newUrl");
} else {
$newUrl = "$rUri?$sessionLang";
header("Location: $newUrl");
}
}
}
We make sure $_SESSION['lang'] isset.
Get the current url protocol and uri
Check if $_GET['lang'] isn't already set
Check if the url already contains parameters (strpos($_SERVER[REQUEST_URI], '?')), is so,
append &lang=, otherwise append ?lang= to it.

PHP: Error in url after insert in data base

I have error when send data to database.
First I have URL like:
www.domain.com/container.php?fun=olmaa
and after send data, the URL is as below:
www.domain.com/container.php?fun=olmaa&sent=yes
But, when send again it looks like:
www.domain.com/container.php?fun=olmaa&sent=yes&sent=yes
Why insert &sent=yes again ??
My code is:
$url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
header('Location: '.$url."&sent=yes");
Thank You
Before appending sent param just check it already exist or not by strpos. Try this..
$url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$final_url = (strpos($url, '&sent=yes') == false) ? $url."&sent=yes" : $url;
header('Location: '.$final_url);
Or can use parse_str.
$url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
parse_str($url, $params);
$final_url = (!isset($params['sent'])) ? $url."&sent=yes" : $url;
header('Location: '.$final_url);
The problem is that the second time you try to send the data the url that you are sending them is
www.domain.com/container.php?fun=olmaa&sent=yes
So when you are building the new url in the $_SERVER['REQUEST_URI'] already exists the &sent=yes part and you are appenting it again thats why you see it two times.
The code user3659034 gave solves it.

PHP: How do I get the URL a file is in?

I can't seem to find a super-global for this. Basically, if a PHP file is executed on, say, http://www.example.com/services/page.php, I would like to retrieve http://example.com/services. How do I achieve this?
Take a look at $_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI']. HTTP_HOST would contain the host name the resource was requested from an REQUEST_URI URI path and query that was requested.
You can abuse dirname:
<p>
<?php echo $_SERVER["HTTP_HOST"] . dirname($_SERVER["REQUEST_URI"]) ?>
</p>
$scheme = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https://" : "http://";
echo $scheme . str_replace(basename(__file__), '', $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']);

Categories