This is regarding FINDING the POSTed data with a PHP script. Where is it? Why do I keep coming up with NULL values when I look where it is supposed to be? Well, it depends on how the data was posted, and if some other site is posting the data you really don't know which method they will use. Here are the first three places to look:
print_r($_POST);
if(!isset($HTTP_RAW_POST_DATA)) $rawpost = file_get_contents( 'php://input' );
echo $GLOBALS['HTTP_RAW_POST_DATA'];
There are more places to hunt, but so far these three have worked for me.
PS With the second method above, when receiving XML, you end up with \" for every ", which can mess you up. Try following up with
$cleanpost = str_replace( '\"', '"', $rawpost );
Wednesday, July 22, 2009
Monday, July 20, 2009
My favorite PHP Class: SimpleXMLElement
1. A lot effort has been expended by countless people to convert XML into PHP arrays. There are countless forum discussions on the subject and many complex scripts to accomplish this task.
But there is something better.
$xml = simplexml_load_string($str);
One line of code converts an XML string to an object of class SimpleXMLElement. Then you can access any individual data element within the object simply by calling the surrounding tags (skipping the outermost [message] tags):
$xml->body->layer1->layer2->layer3
2. It is very useful for debugging objects to have a way of examining the object structure, datatypes, and content. The very useful function for this is var_dump($obj); which lays everything out for you (kind of like print_r($arr); for arrays).
3. Another strange discovery I have made is that instead of enclosing lenghty XML strings in quotes or double quotes, it is possible to dispense with the quotes altogether and enclose it with XML preceded by three "less-than" signs on one end and XML on the other end. I am not sure why this is so or what the ramifications are of this, but it looks like a very useful option.
But there is something better.
$xml = simplexml_load_string($str);
One line of code converts an XML string to an object of class SimpleXMLElement. Then you can access any individual data element within the object simply by calling the surrounding tags (skipping the outermost [message] tags):
$xml->body->layer1->layer2->layer3
2. It is very useful for debugging objects to have a way of examining the object structure, datatypes, and content. The very useful function for this is var_dump($obj); which lays everything out for you (kind of like print_r($arr); for arrays).
3. Another strange discovery I have made is that instead of enclosing lenghty XML strings in quotes or double quotes, it is possible to dispense with the quotes altogether and enclose it with XML preceded by three "less-than" signs on one end and XML on the other end. I am not sure why this is so or what the ramifications are of this, but it looks like a very useful option.
Monday, July 6, 2009
PHP Shortcut of the Day
Normally in PHP you concatenate two strings like this:
$str1 = $str1.$str2;
This adds $str2 to the end of $str1. Here's a handy shortcut, which when constructing really huge XML strings is highly useful:
$str1 .= $str2;
The "dot-equals" operation is even smart and will let you concatenate to a null string at the beginning, which is a good thing when you are in loops. You can't do that in C ; you have to StrCopy and then StrCat. To me, this type of thing is what makes PHP great.
Here's another great PHP shortcut: the 'foreach' loop.
A foreach loop lets you loop through all the elements of an array and get access to either the individual keys and individual data elements. The syntax is:
foreach( $somearray as $key => $data ) { .... }
For multidimensional arrays you can nest foreach loops. It takes some getting used to this funny syntax, but it is very useful and worth knowing about.
$str1 = $str1.$str2;
This adds $str2 to the end of $str1. Here's a handy shortcut, which when constructing really huge XML strings is highly useful:
$str1 .= $str2;
The "dot-equals" operation is even smart and will let you concatenate to a null string at the beginning, which is a good thing when you are in loops. You can't do that in C ; you have to StrCopy and then StrCat. To me, this type of thing is what makes PHP great.
Here's another great PHP shortcut: the 'foreach' loop.
A foreach loop lets you loop through all the elements of an array and get access to either the individual keys and individual data elements. The syntax is:
foreach( $somearray as $key => $data ) { .... }
For multidimensional arrays you can nest foreach loops. It takes some getting used to this funny syntax, but it is very useful and worth knowing about.
HTML - JavaScript : Button Inputs as Links
Normally Button inputs in HTML post data to a destination link specified when the form is declared. Wouldn't it be nice to have multiple buttons that can post to other URL's on the same form? Most "solutions" in forums and books say create a different form for each button, but that's cumbersome and messes up your page layout. And you are not allowed to "nest" forms in HTML.
Enter JavaScript. The one line solution (for GET method): While declaring your button input include:
onClick="window.location.href='../folder/url'"
for POST method dynamically change the form "action" attribute with:
onClick="document.myform.action='../folder/url'; document.myform.submit();"
This last solution is my favorite because you can have a whole bunch of data in input fields automatically posted to whatever URL you want from just one form.
Enter JavaScript. The one line solution (for GET method): While declaring your button input include:
onClick="window.location.href='../folder/url'"
for POST method dynamically change the form "action" attribute with:
onClick="document.myform.action='../folder/url'; document.myform.submit();"
This last solution is my favorite because you can have a whole bunch of data in input fields automatically posted to whatever URL you want from just one form.
Tuesday, June 23, 2009
How to pass variables between HTML documents
The most common way is using the HTTP GET method, which is the default method when you just link to a page using (a href). The disadvantage is that your variables end up visible in the URL such as in www.google.com/search?oq=variable which can be messy and poses some security issues. Also it is sometimes difficult to dynamically change your established (a href) links with user input without a LOT of javascript code. Whatever you pass ends up in receiving document in the PHP $_GET[] array.
The next most common way is by using the HTTP POST method. However, this automaticaly POSTS all of the data in all of the input fields at once, without allowing you to specify priority based on client actions. Results end up in the $_POST[] array.
My preferred method is to set up a bunch of hidden input fields, like (input type="hidden" /) and then use JavaScript events such as onFocus, onBlur, or onClick to modify the contents and the priority of these fields. Then use the standard POST method and the contents of all these fields pops up in the next document in the PHP $_POST[] array. And you can easily debug by temporarily making these fields visible (type="text") and you can see how the javascript events modify your variables as you go in real time.
The next most common way is by using the HTTP POST method. However, this automaticaly POSTS all of the data in all of the input fields at once, without allowing you to specify priority based on client actions. Results end up in the $_POST[] array.
My preferred method is to set up a bunch of hidden input fields, like (input type="hidden" /) and then use JavaScript events such as onFocus, onBlur, or onClick to modify the contents and the priority of these fields. Then use the standard POST method and the contents of all these fields pops up in the next document in the PHP $_POST[] array. And you can easily debug by temporarily making these fields visible (type="text") and you can see how the javascript events modify your variables as you go in real time.
Monday, June 15, 2009
Assorted Useful Discoveries in No Particular Order
Here are a few helpful discoveries that took a lot of time to find but have saved me even more time in the end:
1. There is a PHP function that passes PHP arrays to javascript variable arrays: json_encode($array) It puts the PHP array elements in dble quotes, comma delimits them, surrounds the whole array with brackets, and puts sub-arrays into dot-delimited child elements. If only I had known a few weeks ago...
2. The current state of the art for printing web content wirelessly from an iTouch or iPhone is an app called "Print n Share" from EuroSmartz in New Zealand. I am testing it on an iTouch and it works extremely well.
3. The browser within Print n Share is a watered down Safari-Mini browser that Apple provides third party developers. As such, it lacks certain features, such as the ability to prompt users for their userid and password on secure sites. The workaround is: https://userid:password@www.website.com (I didn't know you could do that!! Actually, you can't do that with IE, for security reasons, unless you tweak your registry).
4. Apple does things in "obvious" ways that sometimes are so "obvious" they take me hours and hours to figure out. Some classic examples: How to eject a CD from a MAC. Where is the damn button?? Oh, there it is. There is an actual eject key on the keyboard !! Or here's another one: how to download an iTouch app? You generally don't do it on your computer!! That solves two hours of fruitless research. You use the "App Store" app right on your iTouch. Or, if you are installing an app that you bought yourself for another person's iTouch, how do you install it (there's the owner's password in the way)? You plug it into your USB port, go open iTunes, find the device, click on it, and then you are presented with an option to install/sync. And finally, my favorite: How to use the AC adaptor to charge it up? It wasn't obvious to me, but I guess it is intuitive to someone: you plug your USB cable directly into the AC adaptor, as if your wall socket was a USB port. Who knows? Isuppose there's data in there somewhere....
1. There is a PHP function that passes PHP arrays to javascript variable arrays: json_encode($array) It puts the PHP array elements in dble quotes, comma delimits them, surrounds the whole array with brackets, and puts sub-arrays into dot-delimited child elements. If only I had known a few weeks ago...
2. The current state of the art for printing web content wirelessly from an iTouch or iPhone is an app called "Print n Share" from EuroSmartz in New Zealand. I am testing it on an iTouch and it works extremely well.
3. The browser within Print n Share is a watered down Safari-Mini browser that Apple provides third party developers. As such, it lacks certain features, such as the ability to prompt users for their userid and password on secure sites. The workaround is: https://userid:password@www.website.com (I didn't know you could do that!! Actually, you can't do that with IE, for security reasons, unless you tweak your registry).
4. Apple does things in "obvious" ways that sometimes are so "obvious" they take me hours and hours to figure out. Some classic examples: How to eject a CD from a MAC. Where is the damn button?? Oh, there it is. There is an actual eject key on the keyboard !! Or here's another one: how to download an iTouch app? You generally don't do it on your computer!! That solves two hours of fruitless research. You use the "App Store" app right on your iTouch. Or, if you are installing an app that you bought yourself for another person's iTouch, how do you install it (there's the owner's password in the way)? You plug it into your USB port, go open iTunes, find the device, click on it, and then you are presented with an option to install/sync. And finally, my favorite: How to use the AC adaptor to charge it up? It wasn't obvious to me, but I guess it is intuitive to someone: you plug your USB cable directly into the AC adaptor, as if your wall socket was a USB port. Who knows? Isuppose there's data in there somewhere....
Tuesday, June 9, 2009
Networking Information
My router at work is set up as a wireless access point, with no physical connection to a PC. It is just hooked to the ethernet cable. It is configured with an encryptation key for security, and it connects to my work domain which I thought was inaccessible to XP home or Vista, until today.
I connected to the domain (yes, domain, not workgroup!) with XP home and with Vista (yes, not XP Pro) wirelessly with the following three simple steps:
1. Connect to the router using the encryptation key and verify internet connectivity.
2. While holding the "Windows Key" on the keyboard down, type the letter "R". This is the "shortcut to 'RUN' ".
3. Enter \\kap-02.companyname.phy (That's just the path to one of the domain computers).
This brings up the domain login which for me is companyname\myfirstinitialmylastname plus domain password. That's it. Then you can surf to the shared folder you need and view files. You can do this with multiple computers on the domain. I did it with a second computer with a shared laser printer. Surfed to the printer, opened it, connected, and selected it a my default printer just as if it was connected to my laptop.
If I find out the router settings I will post that. I did see that one can right click on my computer to bring up "map network drive" to assign say drive Z: to \\mydomaincomputerpath.
I connected to the domain (yes, domain, not workgroup!) with XP home and with Vista (yes, not XP Pro) wirelessly with the following three simple steps:
1. Connect to the router using the encryptation key and verify internet connectivity.
2. While holding the "Windows Key" on the keyboard down, type the letter "R". This is the "shortcut to 'RUN' ".
3. Enter \\kap-02.companyname.phy (That's just the path to one of the domain computers).
This brings up the domain login which for me is companyname\myfirstinitialmylastname plus domain password. That's it. Then you can surf to the shared folder you need and view files. You can do this with multiple computers on the domain. I did it with a second computer with a shared laser printer. Surfed to the printer, opened it, connected, and selected it a my default printer just as if it was connected to my laptop.
If I find out the router settings I will post that. I did see that one can right click on my computer to bring up "map network drive" to assign say drive Z: to \\mydomaincomputerpath.
Subscribe to:
Posts (Atom)