GET method in php clean URL - php

I have successfully created clean url for my project.. now what i need to do is add variables to URL..
localhost/user1/file?action=add
localhost/user2/file2?action=delete
where user and file are already rewritten i dont want it to be like localhost/user/file/add because localhost/user/folder/file will be mistaken to to the action parameter.. please help

Try using the ampersand instead of question mark:
localhost/user2/file2&action=delete
In your htaccess, the rewrite rule might look something like this:
RewriteRule ^user([0-9]+)/file([0-9]+)$ /page\.php?user=$1&file=$2
As you can see, the question mark is already there even though it is masked in the address bar. Appending another variable to the query string would require the ampersand for successful concatenation.

You need to get the url and start parsing the url from the question mark. I would save the contents then to an array, so that you've got a key and a value.
$uri = $_SERVER["REQUEST_URI"];
$questionMark = explode('?',$uri);
$questionMark[1] is the action=delete then. There are probably better ways then using explode() method here, but I just wanted to show how you get the string.

You can read GET variables in PHP by accessing the global $_GET array:
http://php.net/manual/en/reserved.variables.get.php
In your example, the php file that is used for handling files would be able to read in:
echo $_GET['action']; // 'add' or 'delete'

Related

Getting A PHP Variable From The URL (Without A ?) [duplicate]

This question already has answers here:
URL rewriting with PHP
(5 answers)
Closed 7 years ago.
Perhaps I'm using the wrong search terms to try to find this online, but I am trying to accomplish the task of passing a variable in a URL path, with using an identifier.
For example, here is my current URL: http://www.myurl.com/test/index.php?name=bob
On my index.php page, I would set something along the lines of $name = $_GET['name']; and have no issue using this variable.
My goal, however, would be to use the URL: http://www.myurl/test/bob/ and still be able to receive "bob" as the name variable in my script.
Is this possible, hypothetically? Thank you!
put in your htaccess a mod_rewrite statement like
RewriteRule ^test\/([a-z]+)\/?$ index.php?name=$1
One of the easiest ways to do this (and also a bit more secure) is instead of using a GET statement, using a session variable. You could change the url to be whatever you like using mod_rewrite as you have suggested, however you can still access the variable without anything special.
For instance, you just start your session like so
session_start();
then set your session variable, like so (assuming you have already defined $name):
$_SESSION['name'] = $name;
and then on the page where you'd want to get name, put session_start(); at the top of the page, and then, instead of $_GET['name'] just call the variable as $_SESSION['name'] instead.
This way you don't really need to worry about using the URL for passing the variable from one page to another. It won't be affected by rewriting.
Of course, your other option, if you wish to continue using it as a GET variable is this: https://stackoverflow.com/a/8228851/3044080

PHP header_redirect with multiple variables

I am writing a PHP script that will redirect based on a link like this:
header_redirect($_GET['redirect']);
And the URL being:
https://www.url.com/index.php?change_language=english&redirect=other_page.php?category=1234&limit=24
As you can see, the actual page is
https://www.url.com/index.php?change_language=english
and the redirect is then to another page with multiple variables like:
&redirect=other_page.php?category=1234&limit=24
However, when I run the link above, I only get redirected to "other_page.php" and the additional variables are lost.
How would I achieve this?
You can solve this problem using some encryption and decryption trick, I have used base64_encode and base64_decode() function to solve your problem.
Step1: in your html page
<?php $redirectUrl = base64_encode('other_page.php?category=1234&limit=24'); ?>
Link1
Step 2: in your header_redirect() function, you can use base64_decode() function decode the redirect stings and get the expected one.
function header_redirect($redirect){
$redirect = base64_decode($redirect); // you can get the expected redirected url with query string
//your redirect script
}
Depends on what you're trying to do.
Option 1: Use http_build_query.
Try:
$target=$_GET['redirect'];
unset($_GET['redirect']);
$query_str=http_build_query($_GET);
header_redirect($target.'?'.$query_str);
If your starting URL is this:
https://www.url.com/index.php?change_language=english&redirect=other_page.php&category=1234&limit=24
You would then be redirected to:
https://www.url.com/other_page.php?change_language=english&category=1234&limit=24
Option 2: Use rawurlencode and rawurldecode.
However, if your goal is to be redirected to whatever you have stored in $_GET['redirect'] (and ignore any other variables in the URL), then you would need to encode the other_page.php&category=1234&limit=24 bit before you put it into your starting URL. This will effectively escape the special characters and allow you to simply call header_redirect(rawurldecode($_GET['redirect']));.
Say your starting URL is then:
https://www.url.com/index.php?change_language=english&redirect=other_page.php%3Fcategory%3D1234%26limit%3D24
You would then be redirected to:
https://www.url.com/other_page.php?category=1234&limit=24

php document url with parameters in url does not work

I´ve got an url like this www.example.com/v=12345. This will go to my index.php site on my webserver and the variable 'v' is used to do some javascript stuff, which works fine.
Now i want to link to the document 'newsite.php" like this or similar: www.example.com/newsite.php/v=12345. This does not work since the css does not work anymore and i don't know why.. If i do it like www.example.com/newsite.phpv=12345 , the variable is read correctly but it opens the index.php again.. How can i do this correctly?
Thanks guys!
You should pass php variable in url as parameter like this:
www.example.com?v=12345
and in your php just get this value by:
$_GET['v']
It should solve your problems :)
Variables need the seperator ? between url and variable. And if you have more Variables, the variables are seperated with a &
Example:
www.domain.com/?v=123
www.domain.com/script.php?v=123
www.domain.com/?v=123&y=456
And every variable will be available via $_GET[]
$_GET['v']
$_GET['y']

Using link variables in an array to create clean URLs in PHP?

In my php script, I have written code to pull out the url variables and to store them in an array
For eg: if my url is
www/viewgallery.php?cname=Colorado-Fall&pcaption=Light-On-Dunes
Then in my array 'path_info' I have
$path_info[base] = /
$path_info[query_var][cname] = Colorado-Fall
$path_info[query_var][pcaption] = Light-on-Dunes
etc
Now, How can I use this array to make clean urls in my php code?
If I try to write the link as
<a href = viewgallery.php/$path_info[query_var][cname]/$path_info[query_var][pcaption]></a>
it won't take me to the requested page.
Ultimately, I want the url in my address bar to look as
www/viewgallery.php/Colorado-Fall/Light-On-Dunes
How can I make this link work with my php script?
I know I can do this with .htaccess but is it possible to do it just with my php script using my array variables?
This should work:
echo "<a href='viewgallery.php/{$path_info[query_var][cname]}/{$path_info[query_var][pcaption]}'></a>";
In your link you have written it as html. You should write it as php
and also remember to use quote marks on the a tag

How do I use php?=

I'm kind of a noob at this stuff.
But I've been browsing around and I see sites that are kind alike this
www.store.com/product.php?id=123
this is really cool. but How do I do it?
Im stuck using something like this
www.store.com/product/product123.php
If you could tell me how I can go about do this it would be awesome!
What you're looking at is a $_GET argument.
In your PHP code, try writing something like this:
$value = $_GET['foo'];
Then open your page like this:
hello.php?foo=123
This will set $value to 123.
You need to use the $_GET here.
if you use the following:
?id=123
then this will be how to use it and the result
$_GET['id'] (returns the 123)
You can use as many $_GET arguments as you need, for example:
?id=123&foo=bar&type=product
$_GET is an array of what parameters are in the url, so you use it the same way as an array.
Create a file called product.php with this code:
<?php
echo "The argument you passed was: " . $_GET['id'];
?>
Now run this URL in your browser:
http://<yourdomain>/product.php?id=123
and you will understand how $_GET works.
Those are called URL parameters (what they're contained in is called a query string), and they're not unique to PHP but can be accessed in PHP using the $_GET superglobal.
Similarly, you can get POST parameters using the $_POST superglobal, though in POST requests, these parameters are not appended to the URL.
Note: Generally, for usability purposes (and thus also SEO purposes), you want to avoid using query strings as much as possible. These days, the standard practice is to use URL rewriting to display friendly URLs to the user. So your application might accept a URL like:
/products.php?id=32
But the user only sees:
/product/32
You can do this by using mod_rewrite or similar URL rewriting capabilities to turn the friendly URL into the former query string URL internally, without having the user type out the query string.
You might want to have a look at the documentation at www.php.net, especially these pages: http://www.php.net/manual/en/reserved.variables.php
Specifically, have a look at $_GET and $_POST, which are two frequently used ways to transmit information from a browser to the server. (In short, GET-parameters are specified in the URL, as in your question, while POST-parameters are "hidden from view", but can contain more data - typically the contents of forms etc, such as the textbox you posted your question in).

Categories