Get full page URL with PHP - php

I am trying to get the entire page URL as a string in PHP - so, if the requested URL is ./foo.php?arg1=test&arg2=test2, I get "./foo.php?arg1=test&arg2=test2".
I know that I can get the ./foo.php part from $_SERVER and the variables from $_GET, but I was wondering if there's an easy way to do it in just one fell swoop.
TIA.

$url = (isset($_SERVER['HTTPS']) == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
This should return the full url based on what was typed in the address bar.

If I open the following URL in my browser :
http://tests/temp/temp.php?a=145&b=glop
The following piece of code :
var_dump($_SERVER['REQUEST_URI']);
Gives me :
string '/temp/temp.php?a=145&b=glop' (length=27)
So, $_SERVER['REQUEST_URI'] might be what you are looking for...

$_SERVER['REQUEST_URI']
http://au2.php.net/manual/en/reserved.variables.server.php

Related

PHP get full url including the section of the page I want to jump to

I am trying to the full url in php, but am not able to get the section of the page I want to jump to. For example I am trying to get "http://192.168.10.10/#getstarted", but when I try to get the url I only get "http://192.168.10.10/".
Here is my code
$url = sprintf(
'%s://%s/%s',
isset($_SERVER['HTTPS']) ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
);
echo($url);
You can't get the hash of the url from the server side. Hash is available only from the browser side.

HTML::link doesn't point to the right website

I have this:
{!! HTML::link($item->website, $item->website) !!}
$item->website is the website inserted by the user. But, if the user inserts something like www.mysite.com the result is localhost:8000/www.mysite.com
If in database I put http:// in front of the website, I get the right result.
Is there any other way than HTML::link to properly show the links ?
Thanks.
You have to pass a full URL to the HTML::link(); method, else Laravel will automatically prepend the URL with your local website root. In your case you have to add http:// to your variable and pass the whole URL to the method link();
Check this link ;)
http://laravel-recipes.com/recipes/186/generating-a-html-link
HTML::link() expects the first parameter to be a relative path in the current site, or a fully qualified domain name. You need to check if $item->website starts with "http" and if it doesn't, then prepend it to the string.
<?php $prepend = (strpos($this->website, "http") === 0) ? "" : "http://"; ?>
{!! HTML::link($prepend.$this->website, $this->website) !!}
Notice that I'm checking for "http" instead of "http://". This allows "https" websites to work as well.
Url has to be start with "http://".
If user input does not start with it, then you have to add.

Send URL as a Query String

Think my current page url is:
http://example.com/id=10
In this this page has link to go other page, I want to pass current URL as a query string like this:
http://example.com/about-us/?edit=1&return=http://example.com/id=10
in PHP
http://example.com/about-us/?edit=1&return=<?php echo $_SERVER['QUERY_STRING'] ?>
but this is not working, could anyone help me to do this.
Use this (I assume you are using http only);
$currentUrl = urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
$link = "http://example.com/about-us/?edit=1&return=" . $currentUrl;
use urlencode($_SERVER['QUERY_STRING'])
It encodes the link.

How to detect if user go to a link look like our rule?

example:
I have a webpage abc.com/index.php
I want to detect if user connect to abc.com/index.php/bbdh,
abc.com/index.php/bb/saggfd/gjasgd/hsahgd, abc.com/index.php/bb/saggfd/gjas and so on without having a page like that on my host.
I'm using php and need some way to do that without using .htaccess file.
Thanks for any help!
have a look at $_SERVER['REQUEST_URI'], every thing you need will be accessible here. E.g. for:
abc.com/index.php/bb/saggfd/gjas
it would be:
/index.php/bb/saggfd/gjas
strip the /index.php with something like:
echo substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
and you're set.
Update on how to handle GET-parameters:
// get the full request uri
$requestUri = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
// let php parse that url (though it's just a part of the full url)
$chunks = parse_url($requestUri);
// let php parse the query string
parse_str(isset($chunks['query']) ? $chunks['query'] : '', $chunks['query']);
// debug output
print_r($chunks);

PHP - Get string from URL

by having an URL like this: mysite.com/subfolder/helloworld - Is it possible to read the "helloworld" from within a PHP page?
I would like to use the string as a part to load some content.
end( explode( '/', $_SERVER['REQUEST_URI'] ) )
without the end() call it will give you all the parts of the URL
You should read about URL rewriting.
This is more clean than "reading" the current URL. Basically you can redirect your URL (transparently) to something like :
mysite.com/index.php?folder=subfolder&category=helloworld
Then in PHP, you can access the URL parameter with :
$folder = $_GET['subfolder']
$category = $_GET['category']
This is maybe not the kind of answer you were expecting, but it can be interesting to know.
The request url (everything from the first / after the domain name) can be found in $_SERVER["REQUEST_URI"]
Hello have u used 'strstr' keyword of php?
please try like this
if(strstr($_SERVER["REQUEST_URI"], "helloworld"))
return true;
else
return false;
May be this will be helpful to you.

Categories