php

Tag: php

Allowing WWW and Domain Only Access to WordPress Site

Say you have a client who’s come to you to build them a new website; great, congratulations. You do some research and find out that the links on their old site all pointed at the raw domain (no www preceeding it). You’ve also decided that based on their needs, the new site should be set up on the WordPress platform.

Easy peasy, right? Well not so fast there… Let’s say you built the new site, took it live and everyone on the Internet can see the new site BUT your client. More accurately, your client can’t see the site when they are at work but can from any other location. Hmmm…

Read more

What’s so big about OSCommerce?

Sure, they have an extremely large 3rd party/open source community behind them but wow, is this software backward in terms of usability and manageability. I’m doing some work on http://www.forcefieldmagnets.com/ and this is like working with WordPress software from 3-5 years ago. Sure as heck doesn’t hold a candle to ASPDNSF. Perhaps I’m just spoiled?

Anyone have any good, positive comments on OSCommerce? Perhaps because the version I’m working on is so old that is why it seems so limited/backwards.

Using Google Spreadsheets to Collect Form Data

Google Spreadsheets recently offered the ability to create a form that will collect customer information into the spreadsheet. This is great for people that don’t have access to storing data in a traditional database (which is our preferred solution). They offer you the code to either link to for this form, email the code (great for emailed surveys) as well as code to embed (via IFrame) the form in your existing web page.

If you’d prefer to have FULL control over the display of the form and control the formatting and manipulation of data with your server-side code here is a handy little guide on how to do this using PHP.

 

  1. Create Google Doc form with all the fields your form needs
    image
  2. Save the form
    image
  3. Email yourself a copy of the form
    image
  4. Pull the necessary fields out of the email to generate the form
    URL for curl to post to
    URL to post form to...
    Names of the various input fields
    Finding the name of the various fields
  5. Insert code below into PHP page
  6. Test solution
   1: //In order to post to Google Spreadsheet Form

   2: //http://spreadsheets.google.com/formResponse?formkey=dFdYSTlzUVJsSomeReallyLongKeyGoesHereqemU2YUE6MA..

   3: //Name "entry.0.single" 

   4: //Email is "entry.1.single"

   5: //Phone is "entry.2.single"

   6: //Comment is "entry.3.single"

   7: //IP address is "entry.4.single"

   8: $url = 'http://spreadsheets.google.com/formResponse?formkey=dFdYSTlzUVJsSomeReallyLongKeyGoesHereqemU2YUE6MA..';

   9: // Create post array to send results to Google Spreadsheets

  10: $fields = array(

  11:                         'entry.0.single'=>urlencode($name),

  12:                         'entry.1.single'=>urlencode($email),

  13:                         'entry.2.single'=>urlencode($phone),

  14:                         'entry.3.single'=>urlencode($comments),

  15:                         'entry.4.single'=>getRealIpAddr(),

  16:                         'submit'=>'submit'

  17:                 );

  18:  

  19: // Begining of code for posting to Google Spreadsheet

  20: $fields_string = '';

  21: //url-ify the data for the POST

  22: foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }

  23: //rtrim($fields_string,"& ");

  24: $fields_string = substr($fields_string, 0, strlen($fields_string)-1); 

  25: $result = "Fields_String: [" . $fields_string . "]<br />";

  26:  

  27: //set POST variables for Google Spreadsheets

  28: //open connection

  29: $ch = curl_init();

  30:  

  31: //set the url, number of POST vars, POST data

  32: curl_setopt($ch,CURLOPT_URL,$url);

  33: curl_setopt($ch,CURLOPT_POST,count($fields));

  34: curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

  35: curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  36: curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

  37:  

  38: //execute post

  39: $result .= "Curl Results: [" . curl_exec($ch) . "]<br />";

  40:  

  41: //close connection

  42: curl_close($ch);

Things to note about the above PHP code (gasp!) are that you need to define the proper URL to the spreadsheet and the correct number and names of  fields (see step 4 above) . The only one that might be a little odd is getRealIpAddr() method. This is defined elsewhere in the page’s code as follows:

   1: // Function to get real IP address in case visitor is behind a proxy server

   2: function getRealIpAddr()

   3: {

   4:     if (!empty($_SERVER['HTTP_CLIENT_IP'])) // Check IP from shared internet

   5:     {

   6:         $ip = $_SERVER['HTTP_CLIENT_IP'];

   7:     }

   8:     elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) // to check if ip is passed from proxy

   9:     {

  10:         $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

  11:     }

  12:     else

  13:     {

  14:         $ip = $_SERVER['REMOTE_ADDR'];

  15:     }

  16:     return $ip;

  17: }

 

 

 

Have fun folks! If you want to rip into my code and/or have suggestions on better PHP coding styles, please use the comment section below…

Using a Google Spreadsheet as a Database

I recently had the opportunity to attempt to create a simple little CMS for a single page on a website hosted on a Linux box somewhere out there in the cloud. We wanted to offer the client (who had no experience or patience to work in HTML) the ability to update the content on this page. So, with Google’s search engine as my reference material (who needs to buy programming books any more?) I launched into writing some PHP code that will read a published Google Spreadsheet and treat each of the sheets as a table.

Kinda nifty stuff!

Of course, if we are going to do this, I’d prefer the customer use their own spreadsheet so we aren’t held responsible for failure to ensure the existence of this spreadsheet and it’s content. So, here are the steps required to properly publish a Google Spreadsheet that can be consumed by another website:

Create your Google Spreadsheet – In this case, we’ve got 5 columns that contain information to be used in an unordered list typical of a Resources (links) page.

 Publish your Google Spreadsheet – This is the “hardest step” we’d be asking of our client. It entails pulling down the Share button at top right of spreadsheet and selecting Publish as a web page.

image

imageConfigure Published Data Link -  In our scenario, we are publishing All Sheets, Automatically republishing this data when changes are made and we are NOT requiring a login to view this data (making it publicly visible). Once you’ve pressed the  “Start Publishing” button, the bottom half of the form will become active, allowing you to create the correct link to the data in a format that can be programmatically consumed by our target web page. In this case, we want to select CSV data and we are only interested in the “Resource Links” sheet in this spreadsheet. We are taking all available cells. Copy and paste the provided URL and you are good to consume this data via the programming language of your choice.

 

“But  Mike”, you say… “Why not just publish it as HTML to embed in a page” using the aptly named option on this dialog box? Good question. In this case, we want this data to be tied back directly to our client’s site rather than having the content iframed in from spreadsheets.google.com. Better to have this quality content showing up on the clients site for SERP and Page Rank purposes.

“But Mike”, you stammer again… “Why not use the GData APIs that have been written for PHP?” Another good question. In this case, I don’t know if the customer’s hosting environment has the API installed (it installs as an add-on to PHP) nor do I know if the host company will do this for our client even if we begged them to. So this provides us with a nice, lightweight way to address this in any hosting environment that supports later versions of PHP (anything greater than 4.02).

And that’s all there is to it. Perhaps if you are good, and ask really nicely, I’ll make the time to post the source code to consume this data.