Getting values from the query string using Janrain's PHP OpenID library - php

I am using Janrains PHP openID library. All is working perfectly except when I try and pull values from the query string on the script that is returned to after the user is authorized.
Here is the first part of the query string:
http://localhost/Cloudshare/trunk/Cloudshare/php/openID/recieve_auth_request.php? janrain_nonce=2010-11-08T22:29:59Zp9PTgs&openid.ns=http://specs.openid.net/auth/2.0&openid.mode=id_res&openid.op_endpoint=https://www.google.com/accounts/o8/ud.
I am able to get the value of the 'janrain_nonce' item on the query string, but am not able to get any values for things like 'openid.op_endpoint'..
For example, this
$test = $_GET["janrain_nonce"];
returns a value ok into $test, but the the value of $test is blank using the following...
$test = $_GET["openid.claimed_id"];
What I am trying to do is get one of the values from the query string to see if it contains 'google' or not, as I have some processing specific to Google to do.
Any help is much appreciated.
Thanks.

I didn't see openid.claimed_id in your querystring, but try getting $_GET['openid_claimed_id'];
testing your querystring at my API seems to indicate that the "." is being converted to "_" FWIW
I've confirmed that. var.var2 gets converted to var_var2 see
http://api.fatherstorm.com/?query&test_is[]=1&test.is[]=2

Related

JSON won't decode from string in PHP

Good evening,
This is probably a stupid question but I've been fiddling with this for a while now. I have a string coming from an AJAX call. To inspect the actual string that gets sent to my PHP from some JavaScript I put it into the result and spit it out in the front end. The string looks like this:
count: "[{\"cartKey\":\"d9d4f495e875a2e075a1a4a6e1b9770f\",\"qty\":\"3\"},
{\"cartKey\":\"67c6a1e7ce56d3d6fa748ab6d9af3fd7\",\"qty\":\"2\"},
{\"cartKey\":\"f7177163c833dff4b38fc8d2872f1ec6\",\"qty\":\"32\"}]"
So! My problem is getting this to be an actual PHP array. If I do this:
$result['count'] = json_decode($updates, true);
inside my PHP, the result is 0.
Ignore the count name on the result. I'm just trying to turn the above string into an array of objects I can use in PHP rather than a JSON string.
Thanks in advance!
I'm a birk.
The actual value of $updates was null, I think.
I have just solved my problem by not calling JSON.stringify on the data sent from the AJAX call.
The value now comes through and is actually decoded as if by magic within the PHP script.
Thanks for taking a look.
Ash

How to get first part of a URL

can anyone help me to piece together the puzzled I'm facing. Lets say I have url's
/some-work/
/store/bread/alloy/
and in both of these cases I wanna fetch the first part from it. i.e. some-work, store.
Now I've used parse_url(get_permalink()) to get the array of the url and then fetch the path index of the array to fetch the above string. Now I have also checked strstr PHP function, but I am unable to make it work. Can anyone help?
You can use explode, array_filter and current function like as
$url = "http://www.example.com/some-work/";
$extracted = array_filter(explode("/",parse_url($url,PHP_URL_PATH)));
echo current($extracted);//some-work
Demo

Trying to convert a string into an integer in PHP, but it returns 0

I need to get the user's screen width and work with it in PHP, but you can't get the screen width with PHP so I'm using Javascript for that and trying to pass the value as an integer to PHP.
Everything I've read says you need to do that by passing the value to the URL and then PHP can access it through $_GET, but I need to do it in another way. The method I figured out seems to work except for returning a 0 when I try to turn it into an integer.
Here's my code:
<?php
$sWidth = "<script type=\"text/javascript\">document.write(screen.width);</script>";
?>
$sWidth gets the value but it's a string. I've tried a few different casting methods but everything returns 0.
How can I turn the string into an integer using the above method?
PHP is rendered on the server before the data is transferred to the client. You cannot get the screen width without the client specifically sending it to the server say as a GET or POST
parameter.
Hence an ajax call could do the trick. Or a reload with an extra ?swidth=xxx GET parameter.
i.e.
$sWidth = isset($_GET['swidth']) ? $_GET['swidth'] : '';

Echoing string from a mysql query - PHP

How do I echo a simple string from a MySQL Query?
I'm trying trying to accomplish this with the following code but it is not working...The data I am pulling is fine so I know that my mysql_query is working (I've checked that via a different URL GET method.
<?php
$myQuery = mysql_query("fetch some stuff....");
$myResult = mysql_fetch_object($myQuery);
echo $myResult;
you need to know what is returned type. in what your doing you assume that it printable but most of what db queries return are either in object form or an array
try doing a
echo "<pre>" ,print_r($myResult, TRUE),"</pre>";
First of all use var_dump($myResult) to see the data and it's structure.
Since it's an object it will have properties named as the columns returned by the SELECT statement you used.
echo $myResult->column_name; // Should work fine
Usually if echo $variable; doesn't work it means that the variable is either en empty string '' or a null value NULL or a false value FALSE which all show "nothing" when echoed.
But when using var_dump() on them you get a report of the type of data and size of it.
Providing your query is correct, it looks like your php tags are incorrect:
<?php ?>
P.S. It might help if you post the actual query so it can be troubleshooted here. It's hard to ask why something is not working and get an answer if you don't show any of it.
First, var_dump($myResult);. If you see NULL, your query is failing. If you see a big block o' jumbled text, the query is, in fact, working. Since you are echoing $myResult, it is no surprise that nothing is being output, as you are trying to echo the object directly rather than the property you want. Try echoing $myResult->myColumn;
Also, please use MySQLi or PDO, as php_mysql is deprecated.

PHP - Data after "?" in URL displays different information

I know the title isn't very clear. I'm new to PHP, so there might be name for this kind of thing, I'll try to explain as best as I can. Sometimes in a URL, when using PHP, there will be a question mark, followed by data. I'm sorry, I know this is very noobish, but I'm not sure what it's called to look for a tutorial or anything. Here is what I mean:
http://www.website.com/error_messages.php?error_id=0
How do you configure it to display different text depending on what the number is (in this example it's a number)
Could somebody please tell me what this is called and how I could do this? I've been working with PHP for a couple days and I'm lost. Thank you so very much for understanding that I am very new at this.
That "data" is the URL querystring, and it encodes the GET variables of that HTTP request.
Here's more info on query strings: http://en.wikipedia.org/wiki/Query_string
In PHP you access these with the $_GET "super-global" variable:
// http://www.website.com/error%5Fmessages.php?error%5Fid=0
// %5F is a urlencoded '_' character, which your webserver will most likely
// decode before it gets to PHP.
// So ?error%5Fid=0 reaches PHP as the 'error_id' GET variable
$error_id = $_GET['error_id'];
echo $error_id; // this will be 0
The querystring can encode multiple GET variables by separating them with the & character. For example:
?error_id=0&error_message=Something%20bad%20happened
error_id => "0"
error_message => "Something bad happened"
In that example you can also see that spaces are encoded as %20.
Here's more info on "percent encoding": http://en.wikipedia.org/wiki/Percent-encoding
The data after the question mark is called the "query string". It usually contains data in the following format:
param1=value1&param2=value2
Ie, it is a list of key-value pairs, each pair separated with the ampersand character (&). In order to pass special characters in the values, they have to be encoded using URL-encoding format: Using the percent sign (%) followed by two hexadecimal characters representing the character code.
In PHP, parameters passed via the query string are automatically propagated to your script using the super-global variable $_GET:
echo $_GET['param1']; // will produce "value1" for the example above.
The raw, unprocessed query string can be retrieved by the QUERY_STRING server variable:
echo $_SERVER['QUERY_STRING'];
It's called the query string.
In PHP you can access its data via the superglobal $_GET
For example:
http://www.example.com/?hello=world
<?php
// Use htmlspecialchars to prevent cross-site scripting attacks (XSS)
echo htmlspecialchars($_GET['hello']);
?>
If you want to create a query string to append to a URL you can use http_build_query():
$str = http_build_query(array('hello' => 'world'));
As previously described, the data after the ? is the querystring (or GET data), and is accessed using the $_GET variable. The $_GET variable is an array containing the name=value pairs in the querystring.
Here is a breif description of $_GET and an example of it's usage:
http://www.w3schools.com/php/php_get.asp
Data can also be submited to a PHP script as POST data (found in the $_POST variable), which is used for passwords, etc, and is not stored in the URL. The $_REQUEST variable contains both POST and GET data. POST and GET data usually originates from being entered into a web form by a user (but GET data can also come directly from a link to an address, like in your example). More info about using web forms in PHP can be found here:
http://www.w3schools.com/php/php_forms.asp
its called "query string"
and you can retrieve it via $_SERVER["QUERY_STRING"]
or you can loop through $_GET
in this case the error_id, you can check it by something like this
echo $_GET['error_id'];
The term you are looking for is GET. So in php you need to access the GET variables in $_GET['variable_name'], e.g. in the example you gave $_GET['error_id'] will contain the value 0. You can then use this in your logic to echo back different information.
The bit after the question mark is called a Query String. The format is typically, although not necessarily always, key-value pairs, where the pairs are separated by an ampersand (&) and the value is separated from the name by an equals sign (=): ?var1=value1&var2=value2&.... Most web programming environments provide an easy way to access name-value pairs in this format. For example, in PHP, there is a superglobal, which is an associative array of these key-value-pairs. In your example, error_id would be accessible via:
$_GET['error_id']
The reason for the name "GET" is that query string variables are typically associated with a HTTP GET request. POST requests can contain GET variables too, whereas GET requests can't contain POST variables.
As to the rest of your question, you could approach the text issue in a number of ways, the simplest being switching on the error id:
$error_id = isset($_GET['error_id']) ? $_GET['error_id'] : 0;
switch($error_id) {
case 1:
echo "Error 1";
break;
default:
echo "Unknown Error";
break;
}
and more complex ways involve looking up the error message from a file, database or what have you.

Categories