How to retrieve complete URL from address bar using PHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get full URL on the address bar using PHP
I use this function, but it does not work all the time. Can anyone give a hint?
function sofa_get_uri() {
$host = $_SERVER['SERVER_NAME'];
$self = $_SERVER["REQUEST_URI"];
$query = !empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null;
$ref = !empty($query) ? "http://$host$self?$query" : "http://$host$self";
return $ref;
}
I want to retrieve the link in address bar (exactly) to use it to refer user back when he sign out. The urls are different:
http://domain.com/sample/address/?arg=bla
http://domain.com/?show=bla&act=bla&view=bla
http://domain.com/nice/permalinks/setup
But I can't get a function that works on all cases and give me the true referrer.
Hint please.

How about this?
function getAddress() {
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
echo getAddress();

You could use functions above to retrieve URL till GET parameters.
So You have string like = 'localhost/site/tmp' (example).
After that you could just loop through GET parameters if can't get anything else to work.
Add '?' at the end of string manually.
$str = 'localhost/site/tmp/?'
foreach ($_GET as $key => $value) {
$str .= $key.'='.$value.'&';
}
substr_replace($str, "", -1);
echo $str;
At the end You are deleting last symbol which is '&' and is not needed.

Related

How to obtain first subdomain from a domain name [duplicate]

This question already has answers here:
How to get the first subdomain with PHP?
(5 answers)
Closed 3 years ago.
I have the following code:
$url=$_SERVER['HTTP_HOST']; // www.abc.alpha.beta.xyz
$url=strtolower($url);
$rwww=str_replace("www.", "", $url);
But this results in abc.alpha.beta.xyz, the desired result is abc. How can I get just the first subdomain, ignoring www if present?
I think you can use the PHP strpos() function to check if the subdomain URL string contains the word alpha
// Search substring
$key = 'alpha';
$url = 'http://abc.alpha.beta.xyz';
if (strpos($url, $key) !== false) {
echo $key;
}
Not the best solution but might be helpful for you to get started.
There are a lot of ways to do that, a simple one being:
$host = $_SERVER['HTTP_HOST'] ?? 'www.abc.alpha.beta.xyz';
$parts = explode('.', $host);
$answer = null;
foreach ($parts as $subdomain) {
if ($subdomain === 'www') {
continue;
}
$answer = $subdomain;
break;
}
echo "The answer is '$answer'";
Which would output:
The answer is 'abc'
Be aware that this is a very naïve approach and will return example for the input www.example.com - which isn't a subdomain.

Get current page title without loop

I have a small script which should get the meta title of the current page the script is added into. The problem is, that its working fine on several test pages, but not into my CMS. It loops until death there and I cant reach any page on my server until I restart apache completely and by taking the script off.
May someone take a look at it? This would be really awesome since I used google for hours and sure, I found X threads and pages, but never a solution for this special loop-effect.
<?php
function curPageURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
<?php
if (!isset($_GET['ignore']))
{
$url = curPageURL();
$data = implode("", file("$url?ignore=this"));
preg_match ("/<title>([^`]*?)<\/title>/", $data, $match);
$urltitle = $match[1];
}
?>
<?echo $urltitle;?>
The $_SERVER["REQUEST_URI"] can also include GET params like this:
mysite.com?param1=1&param2=2
Then you try to append a string ?ignore=this so you get
mysite.com?param1=1&param2=2?ignore=this
which is translated by PHP into variables like
param1 = '1'
param2 = '2?ignore=this'
You must check for ? symbol in the $url variable
I'm using this function to get the current page url :
function currentURL() {
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['SERVER_NAME'];
$port = $_SERVER["SERVER_PORT"];
$query = $_SERVER['REQUEST_URI'];
return $protocol.'://'.$host.($port != 80 ? ':'.$port : '').$query;
}
But your problem comes from here :
if (!isset($_GET['ignore']))
{
$url = curPageURL();
$data = implode("", file("$url?ignore=this"));
/* ... */
}
This will work with "test pages", but you CMS propably use url-rewriting, which can cause the lost of your $_GET['ignore'] variable : if you've already other GET variable for example.
You should have a look into your .htaccess files, or read your CMS documentation to know what can change you url.
Anyway, it seems you're building some unstable code, and this only to get the page title. I'm pretty sure you've got another way to get it easily with your CMS.

How to reliably get the current URL in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get current page full URL in PHP
I want to do something like this:
if (current url is 'site.com/dir1')
do something;
else if (current url is 'site.com')
do something else;
What's a reliable way to find this out? Thanks.
You can synthesise it with:
'http' . (443 == $_SERVER['SERVER_PORT'] ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
For IIS you don't get REQUEST_URI:
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1);
if (isset($_SERVER['QUERY_STRING'])) {
$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
}
Use $_SERVER['REQUEST_URI']. This should give you the URL that the visitor is using.
if($_SERVER['REQUEST_URI']=="url"){}
Should do the trick.

php add GET at last string

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";

Format a website´s URL as a string with http:// upfront

I have a comment system that allows auto linking of url. I am using cakephp but the solution is more just PHP. here is what is happening.
if the user enters fully qualified url with http:// or https:// everything is fine.
but if they enter www.scoobydoobydoo.com it turns into http://cool-domain.com/www.scoobydoobydoo.com. basically cakephp understands that http|https is an external url so it works with http|https not otherwise.
My idea was to do some kind of str stuff on the url and get it to insert the http if not present. unfortunately whatever i try only makes it worse. I am noob :) any help / pointer is appreciated.
thanks
EDIT: posting solution snippet. may not be the best but thanks to answer at least I have something.
<?php
$proto_scheme = parse_url($webAddress,PHP_URL_SCHEME);
if((!stristr($proto_scheme,'http')) || (!stristr($proto_scheme,'http'))){
$webAddress = 'http://'.$webAddress;
}
?>
$url = "blahblah.com";
// to clarify, this shouldn't be === false, but rather !== 0
if (0 !== strpos($url, 'http://') && 0 !== strpos($url, 'https://')) {
$url = "http://{$url}";
}
Try the parse_url function: http://php.net/manual/en/function.parse-url.php
I think this will help you.
I've had a similar issue, so I created the following php function:
function format_url($url)
{
if(!$url) return null;
$parsed_url = parse_url($url);
$schema = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : 'http://';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
return "$schema$host$path";
}
if you format the following: format_url('abcde.com'), the result will be http://abcde.com.
Here is the regex: https://stackoverflow.com/a/2762083/4374834
p.s. #Vangel, Michael McTiernan's answer is correct, so please, learn your PHP before you say, that something might fail :)

Categories