Get Current Url in PHP instead of files root folder url - php

Hello Friends I want to get the Current Page Url instead of capturing root url of file
I want to capture Like this
http://localhost/tester/
but coming like this
http://localhost/tester/views/inc/readcountry.php
I have tried like this but it's returning
$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]
localhost/tester/views/inc/readcountry.php

This one is working perfectly thanx alot
$_SERVER['QUERY_STRING']

A little handy function I found somewhere else might help you out.
function url() {
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
}
To use it:
echo url();

if you want the full url, try this:
<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
$_SERVER['HTTP_HOST'] - header from the current request, if there is one.
$_SERVER['REQUEST_URI'] - the URI which was given in order to access this page; for instance, '/index.html'.
$_SERVER['QUERY_STRING'] - it is your parameters and values in URL
My htaccess for MVC app:
RewriteEngine on
RewriteRule !.(js|gif|jpg|png|css)$ index.php
Plus in each directory (model, views, controllers) i putted htaccess too:
Deny from all

Not knowing which is the point of the tree you need to stop the capture, I may suggest you to have a look at all the content of the [$_SERVER] array, dumping it, and at the php magic constants.

Related

how to get api token in rest api from url

I'm trying to develop REST Api with php and I have problem to get api token in my php file but it is consider as folder directory when I requested the Url !
Is there any way to solve this problem ?
e.p. Url that I call :
https://example.com/v1/6A4C426B70634E6D3831785155304F566C6359636167485079494C56624C4C524B686136374E6F6D4D51453D/
note : this is api token : 6A4C426B70694E6D3831785155304F566C6359636167485079494C56624C4C524B686136374E6F6D4D51453D
inside my index.php in "v1" folder:
<?php
//get verify vals
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$url = "https://";
else
$url = "http://";
// Append the host(domain name, ip) to the URL.
$url.= $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$url.= $_SERVER['REQUEST_URI'];
echo $url;
var_dump(parse_url($url, PHP_URL_PATH));
?>
thanks
i tried to learn about headers !
The cleanest way to achieve this would be to create a .htaccess file at the root of your server, and add this rewriting rule in it:
RewriteEngine On
RewriteRule ^v([0-1])\/([0-9A-Z]+)\/?$ index.php?version=$1&token=$2 [QSA]
Then, in your index.php file, you will simply get the version number in a $_GET['version'] variable, and the token in a $_GET['token'] variable.
Because you apparently want to anticipate having different versions of your API, you could also do this:
RewriteEngine On
RewriteRule ^v1\/([0-9A-Z]+)\/?$ index-v1.php?token=$1 [QSA]
RewriteRule ^v2\/([0-9A-Z]+)\/?$ index-v2.php?token=$1 [QSA]
In that case, you will only have access to the token in a $_GET['token'] variable, the version number being useless to have in a get variable because you would separate the code of your v1, v2 (etc) or your API in two different files.
If you don't know what these special chains of characters are, they are called "Regex" and I encourage you to read/watch a tutorial about it, you can use them in Apache, PHP, JavaScript, ... many languages, and they are a good way to identify simple to complex patterns of strings.

How to get value after / in URL

I am trying to get the value after the / in a URL in PHP.
I have tried using $_GET['va'], but this only works for the following,
http://localhost:8080/default?va=xxx
What I'm trying to get is this,
http://localhost:8080/default/xxx
How do I get the xxx value after a / in PHP.
Any help is greatly appreciated.
Edit
Thanks to everyone who answered, I wasn't very clear in stating what I wanted. It appears what I was looking for is known as a pretty URL or a clean URL.
I solved this by following Pedro Amaral Couto's answer.
Here is what I did;
I modified my .htaccess file on my Apache server, and added the following code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ default.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ default.php?page=$1
Then I modified my default.php file to GET ['page']
<?php
echo $_GET['page'];
?>
And it returned the value after the "/".
You want to make what is called "pretty URLs" (and other names).
You need to configure the HTTP server appropriately, otherwise you'll get a 404 error. If you're using Apache, that means you may configure .htaccess with RewriteEngine module activated. You also need to add regular expressions.
There's already a question in StackOverflow concerning that subject:
Pretty URLs with .htaccess
Here are another relevant articles that may help:
http://www.desiquintans.com/cleanurls
https://medium.com/#ArthurFinkler/friendly-urls-for-static-php-files-using-htaccess-3264e7622373
You can see how it's done in Wordpress:
https://codex.wordpress.org/Using_Permalinks#Where.27s_my_.htaccess_file.3F
If you follow those, you won't need to change the PHP code, you may use $_GET to retrieve "xxx".
You are looking for: $_SERVER['REQUEST_URI']
The URI which was given in order to access this page; for instance, '/index.html'.
basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
So the $_GET global variable is for parsing the query string.
What you're looking for is the $_SERVER['REQUEST_URI'] global variable:
$url = $_SERVER['REQUEST_URI'];
$url will now contain the full URL of your path. You'll need to use explode('/', $url) to break up that full URL into an array of little strings and parse it from there.
Example:
$pieces = explode('/', $url);
// this will get you the first string value after / in your URL
echo $pieces[0];
You can do in 2 ways.
1- do these steps
Get URL
Explode by /
Get Last element of array
2- Add .htaccess and map that value for some variable
RewriteRule ^default/(.*) page.php?variable=$1
and you can get $_GET['variable']

How to check what is Written in the url bar and navigate to a certain page in php?

What i want to be able to do is if someone writes in the url bar
www.somesite.com/test
test is like the Username name so it will be www.somesite.com/Username
then page navigate to another and display data from the data where the username is the same
The page will navigate to another page and display data from he database
is this possible first of?
And this is what i have write now it not much, and i am also new to PHP
$url = 'http://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url2 = "http://www.somesite.com/"$databaseVar1=mysql_result($result,$i,"username");
if ($url) == ($url2)
{
header("location: http://somesite.com/viewcontent.php");
}
else
{
//not found
}
Am i Able to do something like this? Any help will be great-full thanks
In PHP this is not exactly possible, however, you can do the following:
www.somesite.com/index.php/test
Apache will open index.php and you can use something like $_SERVER['REQUEST_URI'] to navigate from there
You will either need to use a rewrite rule in .htaccess to redirect all requests to index.php. Index.php can then use
Or you can put a ? before the username. Then index.php will automatically get all requests. The $_SERVER['querystring'] will then receive the username.
Taken some assumptions (due to lack of complete source code in your question):
mysql connection is estabilished
proper query has been sent to mysql server
variable $i is set to meaningful value
www server is configured to rewrite requests to index.php
You only have to compare REQUEST_URI with "username" column value taken from database.
Header "Location: ..." must have capital letter "L".
And avoid usage of "==" operator when comparing strings in PHP, strcmp() function is more reliable - "==" operator tests identity not equality - it's useful to compare numeric values.
You can define the index.php as the application dispatcher and create an .htaccess file that will redirect the requests to it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
index.php:
$page = $_SERVER['REQUEST_URI']; //=> '/Users/show'
if ($page == '/some/page/') include('file_that_handles_this_request.php');
Or you could use this to create instances of classes and call methods:
$page = $_SERVER['REQUEST_URI']; //=> '/Users/show'
list($class, $method) = explode($page);
$object = new $class();
$object->{$method}();
you should use mvc architecture to get perfect as you want.
the url in mvc is like same as you want. You can call the method with the parameter from url directly. Here is a url pattern in mvc architecture -
www.yoursitename.com/controller/methodname/parameter

Get string from url example: domain.com/THESTRING

Please some one explain to me how it works and how to do it.
PHP has a function just for this: parse_url().
$parts = parse_url('domain.com/THESTRING');
$path = $parts['path'];
That wil get the part that you want
Could also use below if you want the current URI:
$_SERVER["REQUEST_URI"]
Yet another way:
echo strstr(str_replace('://', '', 'domain.com/THESTRING'), '/'); // THESTRING
EDIT: This should get you started.
Make sure your server support Mod_Rewrite
I opened a .htaccess in the public_html directory.
Then turned on the Mod_Rewrite by this command:
RewriteEngine on
Then I write a new law:
RewriteRule (.*)\.html$ index.php?link=$1
Thats bicycly mean that every url that ends with .html will redirect to index.php
Then in index.php I get the URL link

PHP: using $SERVER[] to return the current directory, regardless of "nice urls"

I am trying to return the current directory of a file in php, regardless what it says in the browser bar. I have tried:
echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
If the url is
http://example.com/directory1/directory2/var1/var2/var3
the above code returns
example.com/directory1/directory2/var1/var2/var3
even though var1/var2/var3 are GET_[] variables tamed by a htaccess RewriteRule. Is there a decent way to get the directory? in the case above I would like to return:
example.com/directory1/directory2/
thanks.
How about replace it with SCRIPT_FILENAME ?
str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname($_SERVER['SCRIPT_FILENAME']));

Categories