I need to pass information via a URL to a PHP function that parses the received data.
So basically when they click on the specific link, it will go to the specific page, but also send through the arguments and values for the waiting PHP function to parse them and depending on what the values are do specific functions.
eg: http://www.mydomain.com/arg=val&arg=val&arg=val (not sure about how I built that URL either)
Your can access the parameters via the array $_GET.
Note: in your example, you basically send just one paramter. PHP cannot handle multiple parameters with the same name.
You can build URLs that contain data from variables using string concatenation or string parsing.
If you browse the web, you will recognize, that the query string (the parameters) always follow a ? in the URL. Read about URLs and the URI syntax.
Probably also interesting for you to read: Variables From External Sources which describes how to deal with data sent via POST or GET or cookies.
Generate URL:
<?php
$data = array(
'first_name' => 'John',
'last_name' => 'Doe',
'motto' => 'Out & About',
'age' => 33
);
$url = 'http://example.com/?' . http_build_query($data);
?>
Pick data:
<?php
$data = array();
if( isset($_GET['first_name']) ){
$data['first_name'] = $_GET['first_name'];
}
// Etc.
?>
You can access url parameters that follow the ? in at the end of the URL using the $_GET superglobal, e.g.
// url: http://mydomain.com/mypage.php?arg=Hello
echo $_GET['arg'];
Parameters passed in the query string are available in the $_GET superglobal array.
You can build the URL like the following (note the ? )
http://www.domain.com/page.php?arg1=val&arg2=val
Then in your PHP using the following code by using REQUEST you can get the data from either GET or POST parameters.
$arg1 = $_REQUEST['arg1'];
Related
I write a simple web server in JavaScript with NodeJS. I have the problem, that PHP doesn't populate $_POST with the passed parameters. What I already achieved is, that $_GET is populated with the passed parameters.
process.env["GATEWAY_INTERFACE"] = "CGI/1.1";
process.env["SCRIPT_FILENAME"] = path.resolve(resource);
process.env["REQUEST_METHOD"] = request[0];
process.env["REDIRECT_STATUS"] = 200;
process.env["QUERY_STRING"] = queryString;
process.env["CONTENT_LENGTH"] = queryString.length;
process.env["CONTENT_TYPE"] = "application/x-www-form-urlencoded";
content = execSync(phpPath +"php-cgi", process.env);
But print_r($_POST) gives me Array ( ). If I use GET it works and the parameters are in $_GET. My assumption is, that the problem is somewhere in the environment variables. queryString.length is the size of the POST parameters given by the browser
I got it working. I had to pipe the query string as stdin input for php-cgi:
content = execSync(phpPath +"php-cgi", { env: process.env, input: queryString });
This only works with POST that's why I check for POST and GET now and set the method accordingly
I am using CodeIgniter 3.x.
User have this data : %7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
User send me the request and pass the data as parameter in url. The last parameter in url is nothing but the users data.
http://localhost/codeigniter/myproject/myfunction/%7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
The encoded data nothing but a json string which he want to encode and then send me the request. If I parse it using php then it yields this : '{"uid":"1234","uname":"kishor"}'
But it is not accepted in url, it gives me Object not found! error in browser.
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
I tried changing $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';, but it doesn't work for me.
Assuming that your CodeIgniter is set up properly and routing things correctly then I recommend sending the JSON as a $_GET parameter:
http://localhost/codeigniter/myproject/myfunction/?json=%7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
So in CodeIgniter, you should be able to simply:
json_decode( $this->input->get( 'json' ) );
If you pass the below way than you have to pass argument in function.
http://localhost/codeigniter/myproject/myfunction/%7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
like this
function myfunction($param = ''){ echo $params; }
The way you are passing is not a proper way. If you need to get and decode that value, use following way in your controller function
i dont know which one is controller & function in your code, I guess myproject is a controller. So, i'm passing 3rd parameter Number 3 in segment
// it will return %7B%5C%22uid%5C%22%3A%5C%221234%5C%22%2C%5C%22uname%5C%22%3A%5C%22kishor%5C%22%7D
$json_value = $this->uri->segment(3);
$json_value = (array) json_decode(stripslashes(urldecode($json_value)));
var_export($json_value);
output
array (
'uid' => '1234',
'uname' => 'kishor',
)
If the incoming PHP possible requests can be the following
1)http://wwww.test.com/api.php?device_id=xxxx&user_name=xxxx&pass_word=xxxx&num_request=xxxx&num_site=xxxx;
how do I extract the user_name field from the above in my api.php code?
Those values are passed via a standard GET request accessible like this:
$deviceid = $_GET['device_id'];
Be aware that this value is unsanitized and open to modification from the user which could ultimately end up with SQL injection depending on your usage of the value.
You can use the $_GET superglobal to acess querystring values
$user_name = $_GET['user_name'];
I'm doing a website. There's a pagination, you click on links and they take you to the page you need, the links pass $_GET variable ( a href="?pn=2" ) and that works fine.
However when i add the category links (also contain $_GET variable
(a href="?sort=english") on the same page, which kind of sort the content on the page, and click it, the system simply overrides the url and deletes all the previous $_GET's.
For example, I'm on page 2 (http://website.com/index.php?pn=2)
and then I click this sorting link and what I'm expecting to get is this (http://website.com/index.php?pn=2&sort=english), but what I get is this:
(http://website.com/index.php?sort=english). It simply overrides the previous $_GET, instead of adding to it!
A relative URI consisting of just a query string will replace the entire existing query string. There is no way to write a URL that will add to an existing query. You have to write the complete query string that you want.
You can maintain the existing string by adding it explicitly:
href="?foo=<?php echo htmlspecialchars($_GET['foo']); ?>&bar=123"
Try using this:
$_SERVER['REQUEST_URI'];
On this link you can see examples. And on this link I have uploaded test document where you can try it yourself, it just prints out this line from above.
EDIT: Although this can help you get the current parameters in URL, I think it's not solution for you. Like Quentin said, you will have to write full link manually and maintain each parameter.
You could create a function that will iterate through your $_GET array and create a query string. Then all you would have to do is change your $_GET array and generate this query string.
Pseudocode (slash I don't really know PHP but here's a good example you should be able to follow):
function create_query_string($array) {
$kvps = array();
for ($key in $array) {
array_push($kvps, "$key=$array[$key]");
}
return "?" . implode("&", $kvps);
}
Usage:
$_GET["sort"] = "english";
$query_string = create_query_string($_GET);
You need to maintain the query parameters when you create the new links. The links on the page should be something like this:
Sort by English
The HTTP protocol is stateless -- it doesn't remember the past. You have to remind it of what the previous HTTP parameters were via PHP or other methods (cookies, etc). In your case, you need to remind it what the current page number is, as in the example above.
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;?>">