Archive for the ‘ PHP ’ Category

Printing multi line XML while maintaining the formatting in PHP

A co-worker of mine introduced me to this tip for maintaining the formatting of multi line information in PHP; in this case it is XML. It uses heredoc. The syntax is basically three <<< and then the name of the type of variable...

Here is a code snippet of it in use:

<?php
 
echo <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<someNode>
	{$massiveAmountOfXML}
</someNode>
XML;
 
?>

HTTP Post in PHP without a Form

For one reason or another I had trouble getting this to work so I thought I would post my code snippet that I used here.

<?php
<?php
ini_set('display_errors', E_ALL);
 
class PostWithOutAForm {
 
	var $_returned_message;
	var $data = array();
 
	public function __construct() {	}
 
	public function callPong($msg) {
		$data['message'] = $msg;
 
		$post_str = '';
		foreach($data as $key=>$val) {
			$post_str .= $key.'='.urlencode($val).'&';
		}
		$post_str = substr($post_str, 0, -1);
 
		// Initialize cURL
		$ch = curl_init('http://localhost:8888/TargetOfPost.php');
		curl_setopt($ch, CURLOPT_POST,				count($data));
		curl_setopt($ch, CURLOPT_POSTFIELDS, 		$post_str);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 	TRUE);
 
		// Execute...
		$output = curl_exec($ch);
 
		 if (!$output) {
			$output = curl_error($ch);
		}
		curl_close($ch);
 
		// Display the result
		return $output;
	}
}
?>

Load Drupal view with Flash and AMFPHP

I recently have been getting into Drupal at work and one of the things we were needing to do was get information out of our Drupal cms install to use in Flash. One method for doing this is to use the NetConnection class in Flash for which I have the code available to download below. In order to do this, setup your Drupal install with the proper modules and permissions.

1. Download and enable a few modules in Drupal.
* AmfPHP
* Services (enable views service)
2. Create a view with the info you want to show in flash
3. Set the proper permissions on your views. (Described in the modules’ documentation)
4. Download my code below, putting in the path to your amfphp install as well as the name of the view you created in step 2.

Note: I had to downgrade to php 5.2.13 in my MAMP install because I was getting the “Function ereg() is deprecated” error but this can be fixed as described in the post if you want to use PHP 5.3+

I am hoping that someone will make a zendamf module since amfphp is not being actively improved.

  NetConnection.zip (24.4 KiB, 124 hits)

Flex and Flash with Zend

In order for this tutorial to work, you need to make sure that your environment is setup by following the instructions located here. I have included an fla (cs3) file in the /Flash folder of the Flex project archive. You would not normally do this for a production project but this is just meant to be a simple example and since they both use the same php files it didnt make much sense to split this article up into two separate articles.

I am currently using Flex Builder 3.0.2 and Flash CS4 but I saved the fla as a cs3 file for those that don’t have it yet.

Code and instructions after the jump Read more

Setup WAMP and the Zend Framework for use with Flash and Flex

There are a few tutorials out there on the net about using Zend with Flash/Flex but most seem to be overly complicated and many don’t work even after following the instructions. (I am hoping that mine will. :) ) The next few articles cover a VERY simple implementation that you can build on for use with future projects and I hope it can save people time when it comes to beginning to explore this new method. This post will deal with setting up your environment to be able to use Zend with Flash/Flex.

Code and instructions after the jump. Read more

Secure way to pass a value to a Form field with PHP

Recently I worked on a project where I had to pass a value from one php page to another. It was very simple and I had it working but didnt think about the security around this functionality as I am still fairly new to php. The biggest issue is to make sure someone cant inject code into the field and mess up your site. Most examples I saw didn’t include this security check, so here you go.

1
2
3
4
5
6
7
8
9
<?php
	//grab email that is passed in from a page
	if (isset ($_GET["email"])) {
	        //htmlentities removes quote characters and html characters from the value passed in
		$email = htmlentities($_GET["email"], ENT_QUOTES);
	} else {
		$email = '';
	}
?>

And here is the code you use to send the value to the page:

<form name="myForm" action="submitForm.php?email=+document.getElementById('emailField').value);">
...
<input type="text" name="emailField" id="emailField" value="email goes here">
<input type="image" src="images/submit.jpg">
</form>

Thanks to PlanetKodiak for the help with this.

 

חוקרים פרטיים

Switch to our mobile site