I have a popup php page that contains this link within the php file (not the browser):
http://mydomain.com/member.php?id=75
how can I get the id value only to define another variables for the users on that page?
I used the $_SERVER['HTTP_REFERER']; to get the link where user came from.
many thanks,
You could just use $getId = explode("=", $_SERVER['HTTP_REFERRER']);
Then set $id = $getId[1] (Since the number is going to be the second position of the array).
Try this code here:
$query=parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
$REFERER_GET=array();
foreach(explode('&', $query) as $kv) {
list($key,$value)=explode('=', $kv);
$REFERER_GET[$key]=$value;
}
echo($REFERER_GET['id']);
id in your url is URL parameter which can be extracted using the $_GET variable.
just try printing $_GET variable. and choose the value of id as ($_GET["id"].
Related
I am trying to store a value obtained from a URL variable into a SESSION variable.
Here is the URL:
Ace Hardware
Here is the SESSION coding, which retrieves the variable, but loses the variable value upon leaving the page.
$_SESSION["store"] = $_GET["store"];
$shopByStore = $_SESSION["store"];
If I plug in the value in quotes as it is below (see "Ace" in code below), it works. But it doesn't work in the code above using the GET method ($_GET["store"])
$_SESSION["store"] = "Ace";
$shopByStore = $_SESSION["store"];
The problem is that you override the $_SESSION["store"] with the $_GET["store"] each time whether the get request exists or not, so it basically only uses $_GET["store"]. You could use this instead:
if (isset($_GET["store"])) {
$_SESSION["store"] = $_GET["store"];
}
At first you need to start the session and then make sure it's already not stored in the session (if it's passed in the $_GET) and if not already saved in the session then store it:
// The first line in your script
session_start();
if (isset($_GET["store"])) {
if (!isset($_SESSION["store"])) {
$_SESSION["store"] = $_GET["store"];
}
}
Read more on PHP manual.
I have coded so that i get the following url upon clicking a certain link.
.../project/auction/auction.php?user=ernie6?auc=1
I just wondered what is best way to "get" the following details "ernie6" (as the username) and "1" this being the first auction. Moreover what is the "general rule" to extract data of the form "y=z?a=b?c=d?..."?
Thanks a lot.
It should be & instead of the second/third/etc ?
user=ernie6&auc=1
and then you can refer to your $_GET global array
To see its full content you can do a var_dump($_GET) or get the specific values by:
$user = $_GET['user'];
$auc = $_GET['auc'];
Your URL is not correct, if you want to provide arguments you need to start with a ? and then separate each arguments by a &
Then on your PHP script auction.php you retrieve each arguments like this:
$user = $_GET['user'];
The $_GET variable is a global array containing every parameters provided on the URL. More info on the query string here : http://en.wikipedia.org/wiki/Query_string
EDIT: If you try to retrieve an argument that does not exist you will have PHP warnings or errors. To avoid these it is better to ensure the index exists in the array before retrieving. Something like this would be better:
if(isset($_GET) && isset($_GET['user'])) {
$user = $_GET['user'];
}
For that url, $_GET['user'] and $_GET['auc'] would be defined in the scope of your script if you properly formatted the link (begin get data with ?, separate variables with &).
Assumimg that you are using GET or POST, you can simply get the values like so:
$user = $_POST['user'];
$auc = $_POST['auc'];
or
$user = $_GET['user'];
$auc = $_GET['auc'];
You might find the following documentation useful:
http://php.net/manual/en/reserved.variables.get.php
http://php.net/manual/en/reserved.variables.post.php
Is it possible once I have retrieved a userid from a MySql database, for example, $userID, to then use that as a parameter in a HTML link, for example;
<href="http://www.site.com/?110938">
Thanks.
In PHP, anything you echo is sent to the browser. So you can simply echo that variable.
echo $userID;
Or to put it in the link:
<href="http://www.site.com/?<?php echo $userID ?>">
Of course that means you're trusting that $userID will contain valid data (and not some malformed HTML attempting to break your site and ruin your user's lives).
If it should always be a number, you can keep out potential bad data by forcing it to be an integer:
<href="http://www.site.com/?<?php echo (int) $userID ?>">
I believe what you are looking to do is use the GET method of retrieving data.
The GET method (as opposed to post) pulls all of the parameters out of the URL. For example, lets take a google maps search url:
http://maps.google.com/maps?q=Empire+State+Building,+NY&hl=en
Let us break down this URL:
http://maps.google.com/ - this is the location of the script
maps? - Maps is the name of the file that contains the script (if
they are using PHP); notice, there is a ? in the URL, indicating
that the GET method is being used. All information after the ?
pertains to the GET data.
q=Empire+State+Building,+NY& - this portion is the first GET
variable. They have decided to use the letter q as the name of
their GET variable, and the value is Empire+State+Building,+NY. The
& indicates that another variable will be specified.
hl=en - this is the second variable in their query.
If they were to use PHP to read these parameters, they would use the following code:
$var1 = $_GET['q']; //Has value Empire+State+Building,+NY as string
$vars = $_GET['hl']; //Has value en as string
If you wanted to have your user's ID be in the URL, and use that to interact with the database, the easiest way would be to use GET, such that your URL would look like this:
www.example.com/example.php?id=45828
Then in your script, you would use the following code to access that data and query the database with it:
$userid = $_GET['id'];
$query = "SELECT * FROM users WHERE userid=$userid;";
$query = mysql_escape_string( $query );
mysql_query( $query );
just pass the variable like <href="http://www.site.com/?var_name=<?php echo $userID;?>">
piece of file1.php:
Add this to DB
This takes the user to a page where the data gets inserted into the database.
file2.php :
$clicked = $_GET['addid'];
$_SESSION['clicked'] = $clicked;
// data gets inserted
header("Location: file1.php?id=$clicked");
But I have got multiple pages (like: file1.php?id=1 | file1.php?id=2 | file1.php?id=3 etc.).
Can the session variable handle multiple numbers? Is there any way to do this?
Any help appreciated.
(P.S.: Currently I am using the GET method to disable the links, but I think SESSION is more reliable.)
(P.P.S.: I need this for a voting script.)
To hold more data in one session variable, you need to create a multi-dimensional array which will hold multiple against $_SESSION['checked']. You can do this like:
$clicked = (int)$_GET['addid'];
$_SESSION['clicked'][$clicked] = true;
// data gets inserted
header("Location: file1.php?id=$clicked");
(also, you should be sanitizing $_GET['addid'].
Then to check if it is set, you can use array_key_exists:
if(array_key_exists($clicked,$_SESSION['clicked'])){
echo "this button should be disabled!";
}
I am not sure I understood right your question, but if it is how to send an array of data with the same id via an http request you can use this syntax for the url
file.php?arr[]=val1&arr[]=val2&arr[]=val3
from your php code you would access the values as a regular array
Can the session variable handle multiple numbers? Is there any way to do this?
The session variable can store an array
$_SESSION['clicked'] this session variable can store one value at a time.
If you want use 2 dimension array to handle multiple values.
$clicked = $_GET['addid'];
Ex: $_SESSION['clicked'][$clicked];
firstly concatinate all the ids with comma in a string as $var = 1,2,3,4 and then pass it using GET.
Then there on that page you can explode it from comma and can store it in array and then foreach loop for the array will do it for you. Hope it works.
i want to pass a url value from a php script to another.I have a database in which i have stored some feeds.I have given some weight values to these feeds and a php script grabs a feed url randomly based on their weights.I want to take the feed url which has been grabbed by the script and then pass this url in another php script where it will be parsed with simplepie in order to show its content.
I am posting the codes of the two files right here:
this is the first script which grabs randomly the feed
http://pastebin.com/2ciQ87Es
this is the second script in which i want to pass the value and makes the parsing of the feed
http://pastebin.com/eN5qG29e
Have you something to recommend??
thanks in advance
Would a $_SESSION not suffice?
In the first script:
session_start();
$_SESSION['session_name'] = 'value';
In the second script:
session_start();
print $_SESSION['session_name'];
On second thoughts, could you not pass the value in a query string, to the second page.
second-page.php?key=value
You could wrap grabbing the feed url from the database in a function, and just include that file like any other php file, and then call that function.
//// feedgrabber.php
<?php
function grabber(){
$query = "SELECT * FROM `feeds`";
//takes all the feed that are declared
$result = mysql_query($query);
$data = array();
while($output = mysql_fetch_assoc($result)) {
$data[] = $output; // assigns feeds to an array called $data, one after the other, in they go!
}
return randomchoice($data); // finds a random feed by calling the function
}
?>
and then on the page where you need it:
require('feedgrabber.php');
$feed = grabber();