PHP - GET parameter - php

i need help with my code:
Im including pages in index.php like:
if(isset($_GET['xx'])
{
// include('yy/'.$_GET['xx'].'.php');
}
and now in the file page.php i wanna use get parameters like from and to.
i use htaccess for short url
RewriteRule ^([a-z]*)$ ./main.php?xx=$1
So i get this url in final:
index.php?xx=page?from=a&to=b
When i print_r($_GET) i got only first parameter xx
$_POST parameter works fine, but i need it with $_GET.

Look at the URL:
index.php?xx=page?from=a&to=b
It should be
index.php?xx=page&from=a&to=b
You put ? in there twice instead of a &

Solved!
by misorude
By specifying your own query string in the replacement, you are
discarding the original one - this is default behavior with
mod_rewrite. You need to specify the QSA flag to keep it.
httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
Thanks for fast responds without reading. Have a nice day!

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

.htaccess re-write use a get variable to replace the filename

This one is a bit tricky for me, I want to make this URL as minimal as possible. Ideally I would like to change this URL:
http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School
to:
http://www.example.co.uk/something-school/
Using .htaccess file.
This means we would be using the school get variable to replace the .asp file name, getting rid of /profile/ as well as the other get variables.
Is this possible? If so how? If not could you potentially give me an alternative?
Any help would be greatly appreciated!
EDIT
A user Badhorsie has wrote a rewrite rule for me which does the exact conversion. Unfortunately the webpage fails to load as these get variables are unfortunately necessary for the page to load.
I am guessing it is not possible to hide the get variables. In which case would it be possible to retain the get variables but to keep them clean? Looks like the directory is also necessary?
Perhaps something like: www.example.co.uk/profile/something-school/1/1/2/3
We can get rid of the school get variable as it is not needed (only needed to replace the profile/profile.asp section.
Try this:
RewriteCond %{QUERY_STRING} (.+(?=&school))school=([\w%-]+)
RewriteRule ^profile/profile.asp /%2?%1 [L,NE,R=301]
Did you tried this RewriteRule. Try adding this code in your .htaccess file.
RewriteRule ^http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School?$ http://www.example.co.uk/something-school/ [L]

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

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

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:

Alternative to $_GET

I've a gallery which should display different albums of pictures.
When I open the gallery with the link, I add the album name with PHP:
<a href="gallery/index.php?album=nature
Then I get the album name with $_GET['album']. It works fine.
But for Me is a clean URL important. And when I am finished with the site and upload it to a hoster, then I change the URL "layout" with the .htaccess file.
So the URL http://www.example.com/gallery/index.php?album=blacknwhite change to http://www.example.com/gallery/.
Now I think with the new URL, $_GET doesn't work anymore. Is there a alternative to $_GET to hand over the album name?
Here's again the code sample:
Site with the link:
D
index.php the PHP part:
<head>
<?PHP
$album = $_GET['album'];
?>
</head>
Your rule should look something like:
RewriteRule ^/([a-z]+)/?$ get_product_by_name.php?product_name=$1 [L]
It should not affect $_GET at all, so if it is not working it's because your rule is not setup correctly.
I would recommend you to keep relevant information in your URL, and the album-title seems relevant in this case as I assume the page is centralized around it?
Make your URL work like this instead:
http://www.example.com/gallery/<album-title>/
Since you already seem to know how to use htaccess, rewrite the URL to your "ugly" one there if you get this type of format. Your URL will look way much better then.
http://www.example.com/gallery/blacknwhite/
You need to look at something called htaccess, as you'll need to use RewriteRule which will make it pretty, but still keep $_GET['album'] available in your PHP.
Yes there is another way but its not recommended. look at http://php.net/manual/en/reserved.variables.server.php
you can use $_SERVER. but the correct way is using RewriteRule that well explained here:
http://www.webdeveloper.com/forum/showthread.php?214564-RewriteRule-with-GET-data

Categories