Extend a link with get variables - php

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!

Related

URL being re-encoded?

I have a few links that look like this:
https://www.example.com/find?category=food%20%26%20drink
Clicking on the link should take me to a page where I can GET the variable, and it SHOULD read "food & drink".
However, when I click the link, it takes me to this url instead:
https://www.example.com/find?category=food%2520%2526%2520drink
the variable reads: food%20%26%20drink.
If I paste the first url into the search-bar directly, it works fine. But if I click on it as a link, then it gets re-encoded somehow.
Any idea how to get it to read "food & drink" even though it comes from a different page?
many thanks in advance!
Realized the links were written as http instead of https.
Consequently, they were being re-written by the htaccess file to https when clicked, and also being re-encoded at the same time.
The link you have is double encoded. The possible solution to this would be
Find line of code where the link getting encoded again and make suer not encode if encoded already. Couple of examples are given here Click Here
If there is no way you can change the code form where the URL is getting generated, then you have to use urldecode twice to parse the url params
<?php
$query = "https://www.example.com/find?category=food%2520%2526%2520drink";
$param = explode("=", $query);
print_r(urldecode(urldecode($param[1])));
?>
Hope this helps!

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

How to pass variables from one php page to another without form?

I want to know how to pass a variable from one page to another in PHP without any form.
What I want to achieve is this:
The user clicks on a link
A variable is passed which contains a string.
The variable can be accessed on the other page so that I can run mysql queries using that variable.
use the get method in the url. If you want to pass over a variable called 'phone' as 0001112222:
<a href='whatever.php?phone=0001112222'>click</a>
then on the next page (whatever.php) you can access this var via:
$_GET['phone']
You want sessions if you have data you want to have the data held for longer than one page.
$_GET for just one page.
<a href='page.php?var=data'>Data link</a>
on page.php
<?php
echo $_GET['var'];
?>
will output: data
You can pass via GET. So if you want to pass the value foobar from PageA.php to PageB.php, call it as PageB.php?value=foobar.
In PageB.php, you can access it this way:
$value = $_GET['value'];
check to make sure the variable is set. Then clean it before using it:
isset($_GET['var'])?$var=mysql_escape_string($_GET['var']):$var='SomeDefaualtValue';
Otherwise, assign it a default value ($var='' is fine) to avoid the error you mentioned.
You can use Ajax calls or $_GET["String"]; Method
If you are trying to access the variable from another PHP file directly, you can include that file with include() or include_once(), giving you access to that variable. Note that this will include the entire first file in the second file.

How I can create advanced PHP link?

I want to create a PHP file with $_GET[] function like this:
http://mywebsite.com/account.php?register -> Register page
http://mywebsite.com/account.php?login -> Login page
It's like a $_GET[] but without variable...
Someone can explain this to me?
<?php
if (isset($_GET['register']))
The variable will be set, but with no value
$_SERVER['QUERY_STRING'] contains the raw query string.

need help in particular php job, dont know how to do it

I want to do something but cant figure out how to do this (i m newbie in php)
suppose, i have a list of URL's which shows live with this preg_replace,
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
my output is like
http://localhost/get.php?url=http://yahoo.com
its obvious that you can view that links at output page, now i want to hide them at front page and make them clickable and working
something like we can change links into variables and then call them by clicking and something works in backgroud which can perform same thing as we are clicking on the link at front page
ya it seems bit confusing :(
you could save the url into a $_SESSION vars and when some users click the link retrive the url from $_SESSION and redirect to it...
//page1 - parse, save link in session and print a call to page2
<?php
session_start();
$_SESSION['url'] = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
....
echo 'GO';
?>
//page2
<?php
session_start();
header('Location: '.$_SESSION['url']);
?>
If i undeerstood what you meant...
Obviously now I used $_SESSION['url'] as a single string, but you can use a multidimensional array intead...
UPDATE:
anyway is better if you use an array on script..
example: http://www.test.org/go.php?page=# (where # is a number)
<?php
$array=("http://www.google.com","http://stackoverfloc.com","ecc");
//you can add more contorl in if statement, like between etc...
if (is_numeric($_GET['page']) header('Location: '.$array[$_GET['page']]);
?>
I'm sorry, maybe I'm not understanding quite well, but, isn't best approch use an array for in side server and use another Get variable to do that?
for example ?link=yahoo
and then
find link in array of url?
BY the way, I'm using NoScript and reports me like a warning..
You build up links with looking like this:
yourdomain.com/redirector.ph?url=#
where # represents an identifier.
In redirector.php you check if you know that identifier and send the redirect HTTP Header:
header("Location: http://www.example.com/");
Important Note:
You may not send any data before sending the header and the code after sending it won't be executed.
Info
http://php.net/manual/en/function.header.php
EDIT
Using this header is not absolutely proper in regards to standard, but it's not too far off, as the response really is at another location.

Categories