Parsing URL Query Strings

Feb 22

Let's suppose you have a URL query string that you would like to remove a parameter from. In my case, I had to retrieve a query string from a table that contained saved searches. Some of the records contained strings where parameters were passed that were no longer used and needed to be removed on the fly.

There are a few functions you will need to use and understand in order to do that:

parse_str(string $str [,array &$arr])
unset(mixed $var [,mixed $var [,mixed $...]])

<?php

Random Quote Generator

Oct 25

Building on our work from a previous entry about arrays, let's create a random quote generator using a function that we'll write.

<?php

//let's first start by declaring an array and assigning
//a few quotes to a number of elements, starting with 1

   $quote=array();
   $quote[1]='beer before liquor, never sicker';
   $quote[2]='stop, hammer time';
   $quote[3]='is this your shoe?';
   $quote[4]='we have no bananas at this time';
   $quote[5]='So i herd u liek mudkipz?';

Arrays and Loops

Oct 25

An array is a data structure consisting of a group of elements that are accessed by indexing. Each element may have an index associated with it that is an integer or a string. Its similar to a variable in that it stores data. The difference is that the data may have many associated elements.

Let's consider a situation where you are trying to sell hula hoops online. You want to offer five different colors. When you build an order form you want to display all of those colors in a drop down list.

You would start by declaring/initializing an array in your code.

PHP and the mail() Function, Part II

Oct 19

This tutorial hows you a little bit about sending HTML in an email, which requires the use of the $headers argument. In addition to showing how to set the FROM field in an email sent via mail(), it additional shows the content-type that must be specified in order to send the message in HTML.

<?php

$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML tags</p>
<table>

PHP and the mail() Function, Part I

Oct 19

The mail() function allows you to send emails directly from a script. This may be useful when trying to send data from an contact form. The behavior of the mail functions is affected by settings in the php.ini file. Talk to you sys admin if you have any issues with getting mail() to work. You need no additional libraries installed as mail() is part of the PHP core.

The syntax/arguments needed to use mail() looks like this:

mail($to, $subject, $message, $headers, $parameters);

The arguments:

$to is required. Specifies the receiver of the email.

Variables

Oct 15

In recognition of the fact that not everyone reading this blog has been programming with PHP since 1999, I'm going to post some pretty basic tutorials from time to time. For starters, let's explore variables.

To assign a value to a variable, you do so using the $ at the beginning of the variable name. After the $ the name can be anything, as long as it doesn't start with a number or use non-alpha numeric characters. The line is terminated using a semi-colon. The following code opens a PHP script block and assigns a value to $x and a value to $y.

<?php

$x = 1;
$y = 2;

?>

Finding Yesterday

Sep 27
I had to write a script today that would query a database to find out the number of new items that went on sale yesterday. Then, for each of those items, I had to match the product with the supplier that had their first items brought into our store on that day. On the days that new suppliers had their first item for sale, we had to generate an email that would notify our customer service people that a new supplier had come online with our distribution system. Before we could do any sort of database query, we had to find out what yesterday was before building a query to find out which items were new.

For the purpose of this example, we'll start off with the first task I had to perform in that script. I needed to find out what yesterday was.

We'll be making use of the mktime() and date() PHP functions, which can be found in the manual. You can find information on the mktime() function and the date() function in the PHP manual. For each of those functions you can learn more about the arguments that you may pass in order to effect the output, such as the format of the actual date.

Copy and past the code below into a file named yesterday.php, upload it to your server running PHP and view the file in your browser. The outcome should look like this:

   01-27-2012

<?php
function get_yesterday(){
   $date_yesterday = mktime(0, 0, 0, date("m") , date("d")-1, date("Y"));
   $format_yesterday = date( "m-d-Y", $date_yesterday);
   return $format_yesterday;
}
   $yesterday = get_yesterday();
   echo $yesterday;
?>

CAPTCHA Authentication with PHP

Sep 27

CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a type of challenge-response test used often during web site account sign-ups to determine whether or not the user is human. This keeps spammers at bay somewhat and prevents malicious users from signing up for multiple accounts. If you use a CAPTCHA system on your web site submission forms, such as the one I outline below, it can reduce the amount of spam you receive and potentially avoid security issues.

A few of the fundamental tasks we'll be performing will be:

How long have you been using PHP?

Sep 05
Less than 1 year
0% (0 votes)
1 -2 years
0% (0 votes)
2 - 3 years
0% (0 votes)
3 -5 years
100% (1 vote)
5 or more years
0% (0 votes)
Total votes: 1

AJAX Core, Part II: How Do I Make a Request?

Jul 27

Making a HTTP request is relatively simple. You tell the XML HTTP request object what sort of HTTP request you want to make, such as HEAD, GET or POST, and which url you want to request. Provide a function to be called when as the request is being made, and finally what, information you want sent back in the body of the request.

The following script makes a GET request for the url "http://rss.news.yahoo.com/rss/politics"

Syndicate content