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.
Related
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 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);
}
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.
If the URL is the following :
If: http://www.imvu-e.com/products/dnr/
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/?
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/index.php
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr/page.php?var=2
Then: http://www.imvu-e.com/products/dnr/
If: http://www.imvu-e.com/products/dnr
Then: http://www.imvu-e.com/products/
How can I do this?
My attempt:
print "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI'])."/";
Have a look at parse_url() function.
It returns anything you need.
Simply print_r() the result from parse_url to see what you get back.
You probably want something like:
$ARRurlParts = parse_url($orgurl);
$newURL = $ARRelem["scheme"].
"://".$ARRelem["host"].
((isset($ARRelem["port"]))?":".$ARRelem["port"]:"").
$ARRelem["path"];
The issue with your "attempt" is that $_SERVER['REQUEST_URI'] will contain everything the user passed, including index.php and question mark and possibly more. In order to get what you are after, you need to parse the $_SERVER['REQUEST_URI']:
If it ends with a slash /, leave it as it it
Otherwise, find the last slash in the string and take the substring from the beginning up to and including this slash
Finally append the result onto the http:// (or https:// with the domain name)
Ended up going with this
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
$address = $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
$parseUrl = parse_url(trim($address));
$parent = (substr($parseUrl['path'], -1) == '/') ? $parseUrl['path'] : dirname($parseUrl['path']) . "/";
return $parseUrl['scheme'] . '://' . $parseUrl['host'] . $parseUrl['port'] . $parent;
Inspired in part by Erwin Moller's answer (Why I voted it) and snipplets across web.
You can strip everything from the last backslash till the end of the string. I am pretty sure that dirname($_SERVER['REQUEST_URI']) won't do the job. You can also try using dirname($_SERVER['SCRIPT_FILENAME']). The last shoud work if you don't have some fancy .htaccess rewrite rules.
i want to add utm_source=twitter in the of the links
i have a link let say
http://abcd.com/news?id=1
it need to be http://abcd.com/news?id=1&utm_source=twitter
if http://abcd.com/news/1
it need to be
http://abcd.com/news/1?utm_source=twitter
any idea?
To check if your link already has URL parameters on the end of it, look for the ? character in the URL. If it's there, use a & instead.
$link = 'http://abcd.com/news?id=1'; // or http://abcd.com/news
$join_char = strpos($string, '?') !== -1 ? '&' : '?'; // determine if we need & or ?
$link .= $join_char . 'utm_source=twitter';
You can check if the URL already contains a query string and branch your logic accordingly:
if (strpos($url, '?') === FALSE) {
$url .= '?utm_source=twitter';
} else {
$url .= '&utm_source=twitter';
}
If you're simply adding it to the end of a link it would look something like
$link . "?utm_source=twitter";