PHP: Getting the url's value which is not parameter - php

As I know the only way to get a value from the url is to use the $_GET[]; . To do this the url must be of the format: http://domain.com/index.php?var1=value1
And then to do the following:
<?php
$value1 = $_GET['var1'];
echo($value1);
?>
What I really want is to get the value of the url in the format:
http://domain.com/value1
I know that is not a really php job, but while my index is index.php can I get the value1 from the url? Using something different rather than HTTP Get method.
Cheers.
[SOLUTION]:
I use the URL rewriting to make it work as #KevBot suggest. So what I did:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?value=$1 [L]

It's called URL rewriting. You need to modify .htaccess.
Here's a great tutorial:

Related

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']

mod_rewrite from URL to GET variable

If someone tries to access this page:
www.myhomepage.com/m/40921
I want the URL (above) to remain the same but the content should be from this page:
www.myhomepage.com/msg?t=40921
If it must be exactly like you said, then use this
RewriteRule ^m/([0-9]+)\/?$ msg?t=$1
But I think you made a typing error. Shouldn't it be msg.php? If so, use this:
RewriteRule ^m/([0-9]+)\/?$ msg.php?t=$1
And in your php file, you can get the t with $_GET['t'].

Clean url in php using htaccess

Clean url in php using htaccess
Here is an example
http://www.example.com/Mobiles/index.php?idd=4
and i want result like this
http://www.example.com/Mobiles
Please help
This is called URL Rewrite. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:
http://www.example.com/Mobiles/4
When user will visit this link .htaccess file will internally rewrite this URL to:
http://www.example.com/Mobiles/index.php?id=4
In this way you can then retrieve id from your PHP like this:
$id = $_GET['id'];
or:
extract($_GET);
This extract function will create the variables automatically from the parameters name and you can access it directly with $id variable.
Here is the .htaccess code:
RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1
In case if you don't need URL like http://www.example.com/Mobiles/4, then use this:
RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4

How to make HTACCESS URL with getting other variabel PHP

I get problem while implement .htaccess file,
for general htaccess like : sitename.com/id/other_var its working very well to getting $_GET Request,
RewriteRule ^sitename.com/id/other_var$ index.php?id=$1&othervar=$2 [L]
my problem is, I need to implement like : sitename.com/id/other?a=x&b=y ,
how it work on htaccess ?
thank you for helping!
Mod Rewrite can use simple regex statements to achieve this..
RewriteRule ^sitename.com/([0-9]+)/(.*)$ index.php?id=$1&othervar=$2 [L]
The above would pass an integer value for id and allow anything passed for othervar

Get full URL using htaccess

I want get the full url of a page request using htaccess
My code is as follows
RewriteEngine On
RewriteRule ^(.*)\.html$ session_page2.php?page=$1 [NC,L]
But in session_page2.php the $GET variable prints as follows
Array
(
[page] => rfiregtoday
)
where my actual url is http://localhost/xxxx/rfiregtoday.html#2009CalgaryWEBC
I want to get rfiregtoday.html#2009CalgaryWEBC any idea how to do this?
You can use $_SERVER['REQUEST_URI'] to get the original request path. This will not include the hash because that is never given to the server in an http request. I suggest you change it to a get parameter like mypage.html?hash=foobar.
If you want the GET parameters available in PHP after a redirect, use the QSA flag in your RewriteRule. This will add GET parameters to the rewrite, and make them available in PHP with $_GET.
More info about the QSA flag here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Categories