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
Related
I know that CakePHP params easily extracts values from an URL like this one:
http://www.example.com/tester/retrieve_test/good/1/accepted/active
I need to extract values from an URL like this:
http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
I only need the value from this id:
id=1yOhjvRQBgY
I know that in normal PHP $_GET will retrieve this easally, bhut I cant get it to insert the value into my DB, i used this code:
$html->input('Listing/vt_tour', array('value'=>$_GET["id"], 'type'=>'hidden'))
Any ideas guys?
Use this way
echo $this->params['url']['id'];
it's here on cakephp manual http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params
You didn't specify the cake version you are using. please always do so. not mentioning it will get you lots of false answers because lots of things change during versions.
if you are using the latest 2.3.0 for example you can use the newly added query method:
$id = $this->request->query('id'); // clean access using getter method
in your controller.
http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query
but the old ways also work:
$id = $this->request->params->url['id']; // property access
$id = $this->request->params[url]['id']; // array access
you cannot use named since
$id = $this->request->params['named']['id'] // WRONG
would require your url to be www.example.com/tester/retrieve_test/good/id:012345.
so the answer of havelock is incorrect
then pass your id on to the form defaults - or in your case directly to the save statement after the form submitted (no need to use a hidden field here).
$this->request->data['Listing']['vt_tour'] = $id;
//save
if you really need/want to pass it on to the form, use the else block of $this->request->is(post):
if ($this->request->is(post)) {
//validate and save here
} else {
$this->request->data['Listing']['vt_tour'] = $id;
}
Alternatively you could also use the so called named parameters
$id = $this->params['named']['id'];
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"].
I am new to PHP.
I need a help regarding the methods of extracting DB name and table name from the given URL name.
For example, let's say, I have an URL like the one below:
/test.php?db=...&table=.../
How to extract the DB name and table name from this URL using PHP and use the result for other query purposes.
If you mean how to parse an existing URL for it's parameters:
parse_url() and parse_str() will help you strip the components of the url. You will primarily be looking at the following
$elements = parse_url($url);
$kvps = $elements->query;
$db = parse_str($kvps['db']);
$table = parse_str($kvps['table']);
But, if you mean how to GET variables from the current page before render:
<?php
$dbname = $_GET['db'];
$tablename = $_GET['table'];
?>
And yea, there are major security risks involved in opening up 'direct' access to your database this way. Best to obfuscate / encapsulate / wrap your functions in tasks like index.php&addUser=tim instead of index.php&insert=tim&db=boofar&table=users&dbuser=root&dbpassword=secure.
If you're just learning, what you're doing is fine, as long as you realize why it's wrong. If you're coding for production, you really need an alternate solution.
There are two ways to pass variables or data to another page.
GET (via the URL)
and
POST (usually a form submission)
You can alway get via
$_GET
http://php.net/manual/en/reserved.variables.get.php
or
$_POST
http://nl.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;?>">
I need some help figuring out why the following scenario does not work. I'm trying to retrieve a value from the last updated ID in mysql, then pass that value via javascript over to an ajax call which calls a .php page, which also calls another function "ZEND_emaiL" in a different php page.
In the very first php page that retrieves the id from mysql LAST_INSERT_ID(), if I hard code the value "100" it works, but if I use the value returned from LAST_INSERT_ID() it causes a failure.
Here's the php code for the LAST_INSERT_ID():
$sql='SELECT LAST_INSERT_ID();';
$last_updated_id = $db->get_var( $sql );
$last_updated_id = $last_updated_id+0;//make int
echo $last_updated_id; //send output back to the ajax call
var_dump($last_updated_id); ------------->RETURNS **int 149**
if I send back a hard coded "100" like this: echo 100; then it works.
Any ideas? Thanks for your help in advance.
The following are values retrieved from the php page that contains the ZEND_email() function. I grabbed these for debugging purposes hoping it would help.
RETURN VALUES for Hard Coded:
var_dump($n_id);---------->Returns **int 100**
var_dump($sqlresult);----->Returns **resource 24**
var_dump($row);----------->Returns **array of data to parse through**
RETURN VALUES FOR Passed in Variable (Fails):
function ZEND_email($to, $from="", $subject="", $msg="", $notif_id='', $root_dir="")
{
var_dump($notif_id);---------------------->RETURNS **string '100'**
$notif_id = $notif_id+0;//convert to int
var_dump($notif_id);---------------------->RETURNS **int 100**
$n_id = $notif_id;
$xsql = $sql_str->SQL_SELECT_all_notif_attachments($account_id, $n_id);
$sqlresult=mysql_query($xsql);
$row=mysql_fetch_row($sqlresult);
var_dump($n_id);---------------->RETURNS **int 100**
var_dump($sqlresult);----------->RETURNS **resource 24**
var_dump($row);----------------->RETURNS **boolean false**
}
you are aware that you could use mysql_insert_id() ?
see http://uk.php.net/manual/en/function.mysql-insert-id.php which would give you an INT value directly,
btw, to convert a variable to integer you can use:
$foo = (int) $bar
or
$foo = intval($bar);
or
$foo = settype($bar,'int');
Hard to tell from those code snippets. Better check what's going on on the client-side.
E.g. with firebug you can both check the actual response data and step into the javascript code.
Had to scrap this code...couldn't get it all to work with the feature for the app so we dropped it. Thanks for your help.