Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, June 14, 2011

Mobile Browser Detect

This is a great PHP script to detect the type of device that is visiting your site and redirecting to a different version. It detects all major phone operating systems (android, iphone, blackberry, windows, etc.). The script is free for non-profit and only $50 if your company is for profit.

Thursday, July 2, 2009

Easy way to Parse RSS in PHP

Here is some simple code for parsing RSS 2 in PHP. It's so easy, I can't believe it.

<?php
     $doc = new DOMDocument();
   $doc->load('http://www.softarea51.com/rss/windows/Web_Development/XML_CSS_Utilities.xml');
   $arrFeeds = array();
   foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array (
      'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
     'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
     'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
     );
    array_push($arrFeeds, $itemRSS);
   }
?>  

Thanks to: How to parse RSS feeds with PHP

Thursday, May 28, 2009

PHP Mail on Windows Server

I had a weird problem with my PHP mail() function on a windows server. When I was trying to send the emails, the email headers would show up in the body of the email itself. After a lot of frustration, I figured out a solution that worked for me.

After each header, you usually add a "\r\n". It seems like the \n was giving us the problem so I took it off. So after each header I had instead "\r". It worked... so there you go.

For example:

$headers = 'From: someemail@email.com' . "\r";
$headers .= 'MIME-Version: 1.0' . "\r";
$headers .= 'Reply-To: replytoemail@email.com' . "\r";

Tuesday, April 28, 2009

CMS Recommendation: ExpressionEngine

I just wanted to give a quick recommendation to a Content Management System that we've been using and have found to be really great.  With all due respect to the druple or joomla-lovers, Expression Engine is a much cleaner way to go.  It does come with a premium, but they have a core version for free which is more than enough for most smaller sites.


Check it out.  Like all CMS's there is a learning curve but this CMS gets my recommendation.

Ternary Operator (Short IF Statements) in PHP

This is a cool little 'trick' if you want a quick IF statement in PHP.  You'd use the Ternary Operator which would evaluate similar to the IF operator.

Syntax is like this:

// Example usage for: Ternary Operator
$action = ("john" != "frank") ? 'john isnt frank' 'john is frank';


So in this case, the output 'john isnt frank' would be saved to $action.

The first portion of the before the ? is what is being evaluated.  So we check if the string "john" is not equal to "frank".

Between the ? and the : is what to do if we returned TRUE.

Between the : and the ; is what to do if we returned FALSE.

Since I have $action = at the beginning, the output is saved to that variable.

For more info on this, see the PHP documentation: http://uk2.php.net/operators.comparison (you'll need to scroll down a little)

Wednesday, March 11, 2009

Editing .htaccess with PHP

Although this isn't necessarily recommended, sometimes it is needed.  Basically, we'd use PHP to edit our htaccess document to set permissions or redirects or whatever else.

Here is a post about doing it: Editing .htaccess with Php - Dev Shed

Thursday, February 12, 2009

Monday, January 26, 2009

Autocomplete textfields

In my search for a cool autocomplete script for my text fields, I came across one that I'd actually recommend.  If you aren't familiar with autocomplete scripts, just start typing a url into the latest version of firefox and you'll see what I mean.


The script uses jQuery, which is a javascript library.  It is really simple to use, everything is included in the download.  It comes with a demo page and you can use to make it work the way you want it.

Wednesday, January 14, 2009

IE6 Problem with Form Submit Image Buttons

A lot of times we use images as our submit buttons for forms. I'm sure you've seen it a lot. Well, say you wanted to test if the form was submitted and used the standard way, lets say in PHP, of seeing if the submit button was sent, for example like this:

if (isset($_REQUEST['mysubmitbutton']))
{ ...}

With IE6 this wouldn't work if your submit button was an image. So if you had this as your submit button:

<input name="mysubmitbutton" type="image" value="1" src="someimage.png" alt="" />

Then your script wouldn't work. The reason is that IE6 WILL NOT send over the variable mysubmitbutton.

The workaround is to know that IE6 will send over other information (along with all other browsers). It sends the x and y coordinates of where you clicked on the image. So you'll see mysubmitbutton_x and mysubmitbutton_y. So if you modify your code to look for either of these variables, you'll be good to go!

Credit is to be given to this discussion forum: Differences in form handling btw Mozilla and IE?

Thursday, January 8, 2009

Get referrer with PHP

Here is some code to find out the referrer to a page in PHP.  This can be very useful if you want to redirect back after login or something like that.  You can also set it up to make sure a visitor came to the page from within your site or something like that.

<?php
session_start();

if(!isset($_SESSION[’referrer’])){

//get the referrer
if ($_SERVER[’HTTP_REFERER’]){
$referrer = $_SERVER[’HTTP_REFERER’];
}else{
$referrer = “unknown”;

}
//save it in a session
$_SESSION[’referrer’] = $referrer; // store session data
}
?>


Hope this is useful.  The reference is: Get a referral URL from the session using PHP

Tuesday, December 16, 2008

Server Root Path with PHP

Here is a simple way to get the server root path with PHP:

$_SERVER['DOCUMENT_ROOT']

Say you want to check if a file exists, you could do this:

file_exists($_SERVER['DOCUMENT_ROOT']."/yourfolder/yourfile.extension")

Putting this in an IF statement will see if the file exists.  If it does, file_exists will return true, else it will return false.

Friday, December 5, 2008

Trim Whitespace in PHP

Trim is a nice built-in function for PHP.  My default it will trim off the whitespace from the beginning and end of a string.

Description

string trim ( string $str [, string $charlist ] )

This function returns a string with whitespace stripped from the beginning and end of str . Without the second parameter, trim() will strip these characters:

  • " " (ASCII 32 (0x20)), an ordinary space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NUL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.      

Returning Content to PHP Variable with cURL

I have a few examples of using cURL here in PHP but this parameter allows you to store the content of the selected source to a PHP variable.

Say, for example, you are loading up a page with cURL and want that information store in a PHP variable to use throughout your site.  You can do this by adding the following code to your cURL request:

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); // return the page content

So here is a full block of code:

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'URL_TO_THE_PAGE');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$savedvalue = curl_exec($curl_handle);
curl_close($curl_handle);

In this case, $savedvalue will hold all the information returned by that page request.

cURL is very powerful and worth learning about.  Here is a good resource: PHP login/download file - CURL libraries.

Thursday, December 4, 2008

PHP Header Redirect

Most people may already know this but for some reason I always forget the syntax (I know... its simple...).  So here is how you can redirect to a page with PHP.

Remember: You have to redirect BEFORE loading any HTML tags or header information.  This should always be tested before ANY echo/print tags or any output.

header('Location: http://www.example.com/');
exit;

Don't forget the exit, this prevents the rest of the page from loading and possibly stopping your redirect.

Thursday, November 20, 2008

Escape Character in MSSQL

I had a frustrating time trying to escape single quotes in MSSQL.  I had enclosed my content in single quotes so that double quotes would be okay, but then I had to deal with if a user put a single quote in the field.  In standard MySQL you could just use the backslash (\) but MSSQL requires a single quote (') in order to escape a character!

Here is a little function I made in PHP to look for single quotes and doubling them so that they would be escaped (thus allowing the content to be inserted into MSSQL).

//********************************
// fix quotes for mssql
//********************************
function mssql_quote_fix($text)
{
$text = str_replace("'", "''", $text);
return $text;
}

Just call this function on the text you're trying to insert.

I'll give a little credit to this article for helping me: Escape Character In Microsoft SQL Server 2000

Wednesday, November 19, 2008

cURL Alternative to PHP Include (with SESSIONs)

This is followup to my previous post about cURL Alernative to PHP Include.  Basically, the method I posted before didn't copy over the current $_SESSION in PHP to the page that you were calling.  This mean, that page didn't know the session (i.e. if someone was logged in).

The following code over comes this problem and passes along PHP's $_SESSION information to the called page.  This has been tested for pages on the same server, I'm not sure how it will work if calling a page from another server.

<!-- save $_SESSION to use in call -->
session_start();
$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
session_write_close();

<!-- Begin cURL call-->
$curl_handle = curl_init('enter_external_url_here');
curl_setopt( $curl_handle, CURLOPT_COOKIE, $strCookie );
curl_exec($curl_handle);
curl_close($curl_handle);

Tuesday, November 18, 2008

cURL Alternative to PHP Include

cURL is usually installed with PHP on a server.  I ran into a problem where I needed to be able to include external URLs into a website.  The server I was using didn't allow for urls in the include field (allow_url_include was off).  So, I used this bit of code to do the same thing:

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'enter_external_url_here');
curl_exec($curl_handle);
curl_close($curl_handle);

Make sure to replace enter_external_url_here with the actual URL.  Quotes are required around the URL.

Monday, October 27, 2008

MySQL DATETIME and PHP

If you ever had trouble with the date conversions between MySQL's DATETIME field and PHP's date function, here is a way to deal with them.

If you want to take the date from MySQL and display it a certain way with the DATE function in PHP you first need to convert it.  Use the following code:

date(formatstring, strtotime($rowentry));

formatstring is your format string for your date (see date reference)
strtotime is a function in PHP that takes a string
$rowentry is the DATETIME data from your MySQL database

Thursday, September 4, 2008

PHP Cheat Sheet

This is an interesting little cheat sheet for PHP.  It doesn't cover a whole lot of functions, but it does have some that are used often.  Might save you  a lot of time.

Friday, August 29, 2008

Displaying Large Text with PHP/ASP from SQL

This is an issue I faced with both PHP and ASP. When I set a field type in SQL to varchar(max) or some large amount, when I attempt to display this information via the server-side language, nothing displays.

I found online an explanation for ASP:

After some testing I found there are no SQL Server 2005 data types
that can be read by an ASP page that hold more than 8000 characters. I
speculate that the 2-byte field length indicator is not read by ASP.
Maybe the field length of the old Access memo data type was encoded
differently. - kirkatsfw-ga
I also found something similar related to PHP. My solution in both cases was as stated above, to reduce the limit for the field to something that the server-side languages could deal with. This was the case with both PHP and ASP along with both MySQL and MSSQL.