I have created a very simple script to extract some parameters from the url using:
$_SERVER['QUERY_STRING'];
I have some values like this link:
http://test.com/r.php?gsgsg&0&1&0&sfs
The values are gsgsg 0 1 0 sfs.
Everything is fine when I use the link on own browser.
But when I press the link on a facebook post. I will only get:
gsgs 0 sfs
It seems like facebook removes my numbers.
I tried printing out the url I'm using to post the message in my app, all the values are there. But when I go in Facebook app and press the link, I am missing 2 int values.
Some one have a hint?
A query string like this:
gsgsg&0&1&0&sfs
Is interpreted as:
gsgsg=&0=&1=&0=&sfs=
Just from that you can already imagine that the value of the first 0 gets clobbered by the next. Why Facebook misses the 1 value is unclear, but it's common sense to apply variable naming rules to query string parameters.
I'm not sure what you're trying to do, but this would be much more reliable:
s[]=gsgsg&s[]=0&s[]=1&s[]=0&s[]=sfs
It generates an array $_GET['s'] of:
Array(
[0] => gsgsg
[1] => 0
[2] => 1
[3] => 0
[4] => sfs
)
Related
I have class named Country->Gov->Branch. Each one of them returns an array to the other in this order : Country gives Gov and merges it with gov, gov gives branch and merges it with branch. Branch array is an attribute of User class when I use User and load it into $_session , I do merge the Branch array to session directly using array merge to make it easy to use so I dont have to loop array of array (Just an idea). The result of print_r($_SESSION); was as follows
Array ( [userid] => 1
[login] => email#gmail.com
[name] => Mohamed Talaat
[group] => 0
[picture] => mac
[Branchbranchid] => 1
[Branchbranchname] => Head Office
[Branchaddress] => xxxxxx st. , xxxx
[Branchgovid] => 2
[Govgovid] => 2
[Govgovcode] => 03
[Govgovname] => Alexandria
[Govcountryid] => 1
[Countrycountryid] => 1
[Countrycountrycode] => 20
[Countrycountryname] => Egypt )
I said Horray , but the happy feelings don`t last forever when I call it in the way below, I get an error.
echo $_SESSION['Branchbranchid'];
Notice: Undefined index: Branchbranchid in D:\xampp\htdocs\office\home.php on line 7
and nothing is echoed
I do have a session_start(); at the first head of the page before any code , i notice the problem happened at the merged part starting from the key [Branchbranchid] because if i do echo $_session['any element']; like
echo $_SESSION['name'];
Before this key ([Branchbranchid]), the code works fine. I tried to turn around by adding values with keys to another array but I get the same result and the same notice appear and nothing is echoed. I think there is a problem in the keys because when I have pushed the values to an indexed array, its working fine but without the keys, it is a lost case ...
Any ideas ... about why and How to avoid or to fix this issue
You should put start_session() as first line in your php file wherever you are trying to access the global session.
And change the $_session with $_SESSION
I use this jquery plugin to manage currency input from autoNumber .
From the input, I got the string like this : 30,103,437.60
Now in POST, I have like this :
Array
(
[0] => Array
(
[ID_INVOICE] => 83
[DESCRIPTION] => REPAIR
[QUANTITY] => 1
[PRICE] => 1,523,000.50
)
)
Please see in [PRICE] . In database, I set PRICE FIELD to DECIMAL(10,2). So, I got 1.00 in my database. I think because the comma on POST->PRICE = 1,523,000.50.
My question is, how can I save this on database with right format ?
I am using PHP.
Your actual number should always be separate from the formatting so that you can show it to different users using the locale appropriate for formatting. Use number_format, money_format etc. in php and use the appropriate locale to go to and fro from number to formatted number and back. Even if you are using a jQuery plugin for formatting, I don't understand why it has to make its way to the database as is.
I have an array such as:
$var = array('hi','ho',rand(2,5));
What I would like to echo is the entire array, exactly as written.
Normally when you try a print_r, it shows as:
Array (
[0] => hi
[1] => ho
[2] => 3
)
But I want:
Array (
[0] => hi
[1] => ho
[2] => rand(2,5)
)
You can get this with file_get_contents, but is there any way to do so within the actual PHP file?
I don't think it's possible because when array is created, random value is assigned to element with index 2 and you cannot check how this value was created.
I don't think it's possible, since the rand is already evaluated as soon as you set the array to some variable.
A workaround would be the hold the expression as a string and then eval it when you need it. Like this:
$varStr = "array('hi','ho',rand(2,5))";
echo $varStr;
// when you actually need it
$var = eval($varStr);
However, this is almost never a good idea. Providing a use-case where you need this might help come up with a better solution.
Let's say I have an address http://example.org/articles/children/title. Now, mod_rewrite translates it into http://example.org?type=article&category=children&id=title.
Now, most users only read the first part, ignoring anything that's after question mark. And they should be safe to do so. But if someone links to me using http://example.org/articles/title?something=long_meaningless_string&type=adult&title=othertitle my app sees $type == "adult" when clicking reader believes he went to a children section.
Any safe and sure way to prevent abuse like that? It's especially problematic with well known CMS systems, where attacker knows get variable names and can use it to hit site's reputation.
One way would be to rename your keys, so that they have a [] at the end, like so:
http://example.org?type[]=article&category[]=children&id[]=title
This will result in a $_GET-Array like this:
Array
(
[type] => Array
(
[0] => article
)
[category] => Array
(
[0] => children
)
[id] => Array
(
[0] => title
)
)
If someone appends more type[]-Entries to the query-String as in http://example.org?type[]=article&category[]=children&type[]=adult, it will look like this:
Array
(
[type] => Array
(
[0] => article
[1] => adult
)
[category] => Array
(
[0] => children
)
)
So you can still get the first entry.
If you don't want to append the [] to your keys, you would have to parse the Query-String, which you can get via $_SERVER['QUERY_STRING'], yourself.
Determine a canonical URI for each page. When the page is requested, check if the URI matches the canonical one. If it doesn't, issue a 301 redirect. (e.g. like this (although that isn't PHP)).
I used regexpal.com to test my regexp against the data Wordpress is trying to compare to and it fails, look at this and tell me if you see the problem?
The regexp
"#^json/(.+?)/?([a-zA-Z0-9]*)?$#"
The content to match
json/trips
These works, the previous one doesn't
json/trips/0
json/trips/13
json/fullticket/9805048001130122361809
If I try all these in regexpal they all work, but in wordpress, only the one that doesn't contain the id of the element I want to fetch fails the others work fine.
Interrestingly enough, the $matches return this:
array
0 => string 'json/trips' (length=10)
1 => string 't' (length=1)
2 => string 'rips' (length=4)
Try this regexp instead :
#^json/([^/]+)/?([a-zA-Z0-9]*)?$#
Output :
Array
(
[0] => json/trips
[1] => trips
[2] =>
)
The answer after tweaking the wordpress rewrite rule a bit more ends up being:
data/([^/]+)(/([a-zA-Z0-9\-]*))?$
Note: i changed json to data in the new scenario so i don't mess up the custom post type rules