At the moment a chuck of my site is running off using GETs to direct to a profile or a page. But what if you go to someone's profile page (so that's one GET) and you then click a sub tab on their profile which uses another GET, can you do this:
http://example.com/something.php?xyz=4example=6
I have seen facebook do this however I'm not sure where to look.
An alternate to this would be Javascript however I would rather do it with PHP if possible.
That should be
example.com/something.php?xyz=4&example=6
Note the ampersand '&' between the get vars.
To access the vars in php use
$xyz = $_GET['xyz'];
$example = $_GET['example'];
I am not sure ur exact question but in general if you want to pass values to a page through url u should do e.g.
http://example.com/page1.php?var1=val1&var2=val2....
Please note that each new variable after the first one has "&" before it. This tells the server that a new variable is expected, and the first variable has "?" before it, which tells the server to expect variables.
In the php page you can get the values of all the passed variables like
<?php
$_GET['var1']
$_GET['var2']
.
.
.
?>
and further user the values however you like. Note that you can not change a value in $_GET['var1']. If you want to change a value. First assign this value to a variable then further process. e.g
<?php
$var1 = $_GET['var1'];
$var1++;
?>
Related
How does one access and receive back a variable from a PHP file with a HTTP Request?
I have gotten a HTTP Request to connect to my .PHP file. What I wanted to do next is for example reference that I want to receive $testvariable = 1; back but I have no idea where to begin. The HTTP parameters don't really let me reference this $testvariable directly.
What if the PHP file has simply one function and at the end of it it does a return $testvariable;? Would HTTP receive back this one variable? What if I need more than one. Maybe try and get the PHP to place parameters in the URL and the HTTP reads those parameters in the url? Maybe these "headers" are key to this...
I figured it out. HTTP Request gets back ALL that is on your HTML page. It seems like it ignores <html><body> tags but everything else in the body gets taken back at your response. Even if your answer is " 1 " but you have a single space around it your response is 1 including blank spaces. You need a clean "1".
Passing variables works by using the contents field. You place your values and assign them to a string like number= that then gets posted on the page. When a page is loaded, it can look for a variable under the same name like $test = $_POST['number']; and take the value within that variable and use it further where it needs to on your page.
So basically what I'm trying to do is if the url contents looks something like this:
www.some.com/dir/?variable=VAR&variable2=VAR2.
then the server would pick this up and I could add it to a variable like:
$var = [variable];
echo '<tag>'.$var.'</tag>';
and that would produce;
<tag>VAR</tag>
Sorry for the lack of code I just don't know how to do this with PHP and my searches are turning up blank.
What you want to do is iterate through your $_GET variables and print them out.
$key is variable and $value is VAR . to print key value combo use this. to print just value remove the '$key is ' part.
<?php
foreach($_GET as $key=>$value)
{
echo "<tag>$key is $value</tag>";
}
These variables are called query parameters and in PHP they can be accessed using the $_GET superglobal.
To use your example, the URL www.some.com/dir/?variable=VAR&variable2=VAR2 will populate $_GET['variable'] with 'VAR' and $_GET['variable2'] with 'VAR2'.
You can access $_GET just like any other array from anywhere in your code so it should be straightforward to put its contents in your HTML code:
<tag><?php echo $_GET['variable'] ?></tag>
<tag><?php echo $_GET['variable2'] ?></tag>
Do keep in mind this presents an HTML injection vulnerability. For example, the user could access the URL www.some.com/dir/?variable=<script>doUnfortunateThings()</script> and your script would dutifully render
<tag><script>doUnfortunateThings()</script></tag>
Which would be executed by the browser when the page loads. This might be fine since only the user messing with the URL will see it, but depending on the rest of your page it could pose a security risk, and could even be made permanent by other scripts running on the page or on the server. It could also bypass any content security policy settings your site is running under, depending on how that is configured if at all.
It is good practice to use the built-in PHP function htmlspecialchars on any user input before displaying it on the page to prevent any html tags from actually being rendered by the browser.
<tag><?php echo htmlspecialchars($_GET['variable']) ?></tag>
<tag><?php echo htmlspecialchars($_GET['variable2']) ?></tag>
If you already know variable name from url then you can use it as array index:
$variable = $_GET["var_name"];
In your case:
$variable = $_GET["variable"];
$variable2 = $_GET["variable2"];
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
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.
I have two pages. First one we open with $_POST variables in its url, the second one opens inside first via iframe. Both php files, second is for html manipulation.
Variables I got in $_POST are passed to iframe via $_GET:
echo '<iframe src="index.php&first=' . $first . '&second=' . $second . '&third=' . $third . '&iframe=true"></iframe>';
$first, $second, $third variables has text inside them with some html and new lines (\n).
The problem is, when data is passed to iframe by $_GET, all the new lines in variables disappear.
Tryed to pass variables like base64_encode($first), and then decode them by base64_decode(). It works buggy, some parts of text don't decode correctly, maybe because of bad symbols in iframe url.
Also tryed to throw all the variables into single array, serialize it and then encode by base64 - this way server gives error 500 (it also gives the same error for 404).
Please don't ask me why I did such structure of pages. It should not be changed.
What is the solution for this?
What about an urlencode after the base64_encode?
Depending on your situation you could also use Javascript to access the parent frame.
You could store the data in a javascript array of the first window, then the iframe sub window could call it via parent.*
Some more details from other questions.
You could write the contents of $first,$second,$third to first.txt,second.txt,third.txt and then open the text files inside your iframe script
Your initial approach is wrong.
POST variable shouldn't go anywhere.
After the POST request server have to order browser to reload the page.
Whole page, not only iframe in it.
After that reload you may show any iframes to user.
To pass the data there, a session would be ideal solution.
However, certain solution depends on the data nature and overall purpose of all the mess.