Using link variables in an array to create clean URLs in PHP? - 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

Related

Extend a link with get variables

I have this link:
www.something.com/index.php?page=teams
I then have this link:
Profile
I want to achieve is this link:
www.something.com/index.php?page=teams&profile=5
But I get this instead:
www.something.com?profile=5
The tricky part is that I can't just write the whole "path" like this:
Profile
Because it isn't always on the page 'teams'.
It might be a pretty stupid question, but i can't really figure it out.
You must include the page=teams parameter in the href.
Instead of
Profile
Something like
Profile
If that link is static you must need to update the html file.
But if page=anyvalue then you must generate the href link dynamically in your php code
for example:
Where $page is the current page, e.g $page="teams"
Well, you can just add it to the query string using http_build_query:
$newQueryString = http_build_query(['profile' => 5] + $_GET);
And then output it to your link:
Profile
Try something like this:
echo '<a href="'.{$_SERVER['SCRIPT_NAME']}.{$_SERVER['QUERY_STRING']}
.'&profile=5">Profile</a>
This makes use of $_SERVER to get the current page name (SCRIPT_NAME) and the existing query string (QUERY_STRING), and then appends "&profile=5" onto the end.
Well i have sort of fixed it.... i created a session called page, so i can access it everywhere. The thing is i can't get the variable $_GET['page'] because it's inside a included file called search.php, therefore i created the session.. But thanks for you suggestions guys!

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

Use link url upon click in PHP

I want to use the URL of the link that was clicked in a VB.net program. How can I take the url from my browser url bar and use it in my PHP program?
Example:
VB.net - click link then open using a web browser
url: www.something.com/id=^%$##&var2=13lfhd3f4gt
PHP - put the link in a variable or something so that I can use explode command to get the id and var2 from the URL itself
I need those variables to output a certain value from my database.
This question has the answer that you are looking for, for getting the url in PHP:
Get the full URL in PHP
Though for getting id and var2, it would be simpler to just use the $_GET variable in php. Then you don't have to explode the url and process it. Just change the '/' after 'com' to a ? like:
www.something.com?id=^%$##&var2=13lfhd3f4gt
You can do this using eg. $_SERVER global variable. Please refer to the manual

how do you copy a url into variable

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

GET method in php clean URL

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'

Categories