PHP - Get string from URL - php

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.

Related

how to take the link with its variables from the page

how can i take the link of the page that i am on with its variables ?
example i have the page link is
article.php?article_id=10&article_title=title&lang=ar
when i use the $_SERVER['SCRIPT_NAME'] variable it takes only article.php
and im rewriting the url as well so it looks like this
article/10/title/ar
what i want to do is just make a link that is to an English page so im trying to make it look like this
article/10/title/en
how can i do that?
Since the data looks like its passed with a HTTP GET method, you can use this
$_GET["lang"];
This returns the value assigned to "lang"
$_SERVER['QUERY_STRING'] will have all the parameters. You can also check $_SERVER['REQUEST_URI'] and that should contain the whole url, file and parameters.
Something like:
$params = $_GET;
$params['lang'] = 'en';
$link = basename($_SERVER['SCRIPT_NAME']) . implode('/', $params);

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

Doing a URL re-write while using PHP GET method

When I click on a comment section for a given entry on a site I have, the URL looks like this:
http://www...com/.../comments/index.php?submission=Portugal%20Crushes%20North%20Korea&submissionid=62&url=nytimes.com/2010/06/22/sports/soccer/22portugalgame.html?hpw&countcomments=3&submittor=johnjohn12&submissiondate=2010-06-21%2019:00:07&dispurl=nytimes.com
I want to make it look like this URL:
http://www...com/.../comments/Portugal-Crushes-North-Korea-62
I understand that this involves adding rules to the .htaccess file. I have two questions:
Since I am using the GET method in PHP, the ugly URL has a bunch of variables appended to it. I don't want all of these variables to appear in the clean URL. Is it possible to only include a few of the variables in the clean URL but still have a rule directing it to an ugly URL with all of the variables?
Once I have the .htaccess rules written, do I go back and change the links in the source code to direct to the clean URLs? If so, how do I do this using the GET method when the clean URL does not have all of the variables that I want to pass along?
Thanks in advance,
John
I'm not sure why you need all that data in the URL. You should be storing things like the submission title, its date and author in a database and then refer to it with an ID. That way, your URLs will be shorter and prettier:
http://www.example.org/article.php?id=1
http://www.example.org/article/1/
You can accomplish this with a simple RewriteRule in your .htaccess file, like so:
RewriteEngine On
RewriteRule ^articles/([0-9]+)/ article.php?id=$1
No, you can not leave variables out and expect them to be passed anyway. If you do this, the information is no longer in the URL, so you don't have a way to get it.
You can use post instead of get if you want to pass variables without them showing up in the URL.
I join the word of Sjoerd, but there are a lot of ways how you can rewrite your url like you want to!
Apache and (IIS too) supports url-s like this one: http://example.com/index.php/my-rewritten-url_62
function URISegment($segment)
{
$uri_array = explode('/',$_SERVER['REQUEST_URI']);
$uri_count = count($uri_array);
$returning_uri = array();
for($i = 0;$i<$uri_count;$i++)
{
if(empty($uri_array[$i]) || $uri_array[$i] == "index.php")
unset($uri_array[$i]);
else
array_push($returning_uri,$uri_array[$i]);
}
if($segment < count($returning_uri))
return $returning_uri[$segment];
else
return false;
}
This works, but you need to define the base url too, and this needs to be called at the beginning of the file, and implemented at every image, script, etc. call.
function BaseURL()
{
if(isset($_SERVER['HTTP_HOST']))
{
$base = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$base .= '://'. $_SERVER['HTTP_HOST'];
$base .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
else
{
$base = 'http://localhost/';
}
return $base;
}
After this you can use instead of this:
// http://example.com/?MyKey=Some-data
$MyKey = $_GET['MyKey']; //which is the first item
echo $MyKey;
// results: Some-data
This:
// http://example.com/?MyKey=Some-data
$MyKey = URISegment(0);
echo $MyKey;
// results: Some-data
You've got the same result by each one.
PS:
I like this solution because I can mix url types as I need them like:
example.com/index.php/index/evaled-article?some=db-stored&code=snipplet
And of course you can rewrite your url like FRKT said :)
And of course, if you want to hide the index.php you need to use mod_rewrite, because there's no way

Get full page URL with 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

Categories