I'm trying to copy a url that looks like this: http://domain.com/myfile.php?test-main
So I'm trying to get the url so I can add variable r to the end like so: http://domain.com/myfile.php?test-main?r=stuff
When I use $_SERVER['REQUEST_URI'] it just copies the file name myfile.php and not the other variables. Any one how I can copy the entire url?
$addr = "http://".$_SERVER['SERVER_NAME']."/".$_SERVER['REQUEST_URI']."?".$_SERVER['QUERY_STRING']."&r=stuff";
And you might want to filter variables for a possible XSS attack
Build it yourself out of the provided parts.
Do a print_r($_SERVER); to see all available
The main one you want to add is HTTP_HOST
Related
So I've search far and wide to try and find an example of this but found nothing. It seems like a simple thing however I continue to get errors.
Essentially what I've got is:
<?php
$articleCount = 3;
include('/newsArticles.php?id=$articleCount');
?>
It seems fairly self explanatory. What I need is an include that I can pass a value on into the file which I can then grab with $_GET['id'].
You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['id'] is defined before you call:
include('/newsArticles.php');
Then newsArticles.php will also have access to $_GET['id'];
You don't need to pass a $_GET variable. Just set it, and reference it in your included file.
Your included file will have access to $articleCount without any additional work needed.
Try this:
$_GET['id'] = 3;
include('/newsArticles.php');
Since you are including a script it would seem as though you have access to the script itself. Even if you don't have access to the script you can edit the $_GET variable from within the script you showed above, like this:
<?php
$_GET['id'] = 3; // or $_GET['id'] = $articleCount;
include('/newsArticles.php');
?>
That's because the script newsArticles.php has the same access to the $_GET variable, unless the script was specifically created so that it extracts the variables from the URL.
Try it.
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
So I have this link:
http://kenthomes.net/Amelia-Cove (We use an alias system.)
Then I open a pop-up iframe (http://kenthomes.net/shareplan.php?mod=39)
How I can I pass the string "Amelia-Cove" to that page?
My best guess was to make the link = to http://kenthomes.net/shareplan.php?mod=39&plan=Amelia-Cove
But how to I retrieve only "Amelia-Cove" from the initial page?
Try this:
$_SERVER['REQUEST_URI']
In your above example, this would have a value of: /Amelia-Cove. If you want to get rid of the /, try this:
trim($_SERVER['REQUEST_URI'],'/');
You can use the GET protocol to receive parameters in the URL:
http://kenthomes.net/shareplan.php?mod=39&plan=Amelia-Cove
then fetch these parameters through the PHP GET global variable:
$_GET['plan']
I'm assuming OP will not always fetch Amelia-Cove but rather want that part of the url and used Amelia-Cove as an example and knows how to use $_GET
To fetch that part of the url and pass it through $_GET I would suggest using this:
parse_url($url, PHP_URL_PATH)
Full parse_url documentation
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).
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'