<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Studge &#187; PHP</title>
	<atom:link href="http://studge.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://studge.com</link>
	<description></description>
	<lastBuildDate>Wed, 17 Sep 2008 17:21:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Open Addict Blocks Internet Explorer</title>
		<link>http://studge.com/open-addict-blocks-internet-explorer/</link>
		<comments>http://studge.com/open-addict-blocks-internet-explorer/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 01:08:46 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://studge.com/open-addict-blocks-internet-explorer/</guid>
		<description><![CDATA[Rich Morgan, of Open Addict, has recently made a move to block Microsoft&#8217;s Internet Explorer from rendering his site. This move is justified by the fact the Microsoft does not seem concerned with helping to adopt web standards which causes web developers to have to tweak their sites and implement various, dirty workarounds to look [...]]]></description>
			<content:encoded><![CDATA[<p>Rich Morgan, of <a href="http://openaddict.com/">Open Addict</a>, has recently made a move to block Microsoft&#8217;s Internet Explorer from rendering his site.<span id="more-58"></span> This move is justified by the fact the Microsoft does not seem concerned with helping to adopt web standards which causes web developers to have to tweak their sites and implement various, dirty workarounds to look the way they are intended in IE.</p>
<p>I just updated the theme of this blog and, to no surprise, it does not render correctly in IE 6 or IE 7.  It looks exactly the same in Firefox, Opera and Konqueror though.  At Rich&#8217;s <a href="http://www.openaddict.com/forums/viewtopic.php?p=5512">forum</a>, he suggests that more people rally with him on this cause.  I am not yet prepared to cut IE users out entirely, but it is definitely in my head now.  If you are interested, he has also posted the short PHP code to stop IE from browsing:</p>
<div class="code">
<pre><code>&lt;?php
  if (eregi("MSIE",getenv("HTTP_USER_AGENT")) ||
  eregi("Internet Explorer",getenv("HTTP_USER_AGENT"))) {
    Header("Location: http://www.domain.com/ie_reject.html");
    exit;
  }
?&gt;</code></pre>
</div>
<p>You can place this snippet at the top of any page that you do not want to be seen in an IE browser.  Replace <code>domain.com</code> with your own domain name and be sure to create <code>ie_reject.html</code> with a little message explaining what just happened.  Rich&#8217;s message IE page can be seen <a href="http://www.openaddict.com/ie_reject.html">here</a>.</p>
<p>It would be absolutely amazing if this type of behavior became widespread, but I don&#8217;t have much faith in the movement.  Unfortunately, there are larger revolutions on the horizon.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/open-addict-blocks-internet-explorer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a Site Authentication Login with PHP and MySQL</title>
		<link>http://studge.com/create-a-site-authentication-login-with-php-and-mysql/</link>
		<comments>http://studge.com/create-a-site-authentication-login-with-php-and-mysql/#comments</comments>
		<pubDate>Wed, 23 May 2007 16:11:55 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[(X)HTML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://studge.com/create-a-site-authentication-login-with-php-and-mysql/</guid>
		<description><![CDATA[Setting up restricted access to certain pages is a breeze.  Here I will show a quick way to control who can view specific pages using a form based login system and a MySQL database of allowed users.
First we will set up the database table for storing the user login information.  We will be [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up restricted access to certain pages is a breeze.  Here I will show a quick way to control who can view specific pages using a form based login system and a MySQL database of allowed users.<span id="more-51"></span></p>
<p>First we will set up the database table for storing the user login information.  We will be encrypting the passwords later on, here is the SQL code for the table:
<div class="code">
<pre><code>CREATE TABLE `users` (
  `id` int(4) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM</code></pre>
</div>
<p>I insert the users manually through my phpMyAdmin front-end.  If I am doing a site that has multiple users, then chances are good that I will be using a content management system, such as <a href="http://drupal.org/">Drupal</a>.  I use this authentication method for smaller sites and do not find it necessary to write a script for inserting users, but creating one would be relatively simple.  The one thing you need to be sure to do when inserting a new user is to select MD5 under the function section for the password (see screenshot below).  <a href="http://en.wikipedia.org/wiki/MD5">MD5</a> is a form of encryption that is easy to implement with PHP.</p>
<div class="centered-image"><img class="upped_image" src='http://studge.com/wp-content/uploads/2007/05/screenshot.jpg' alt='phpMyAdmin Screenshot' /></div>
<p>Now we have the MySQL end taken care of and we can focus on securing the pages.  For each page that we only want to be seen by authorized eyes, we need to put in some PHP code.  I put this code snippet as the first text after the <code>&lt;body&gt;</code> tag.  If you are using PHP4, then the code must be the very first text at the top of the web document.
<div class="code">
<pre><code >&lt;?php
  session_start();

  if (!isset($_SESSION['is_logged_in'])) {
    header("Location:login.php");
    die();
    }
?&gt;</code></pre>
</div>
<p>This snippet checks to see if a user has been authenticated for this session.  If not, then the user is redirected to a login form and the script dies.  If the user has been authenticated for this session, then the page continues to display the contents.  Now we need to create a login page.  The code above specifically refers to <code>login.php</code> so we will name the new page just that.  Here is my code for the login page:
<div class="code">
<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
    &lt;title&gt;Login&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;form method="POST" action="check.php"&gt;
      Username:&lt;br /&gt;
      &lt;input type="text" name="username" /&gt;
      &lt;br /&gt;&lt;br /&gt;
      Password:&lt;br /&gt;
      &lt;input type="password" name="password" /&gt;
      &lt;br /&gt;&lt;br /&gt;
      &lt;input type="submit" id="subbut" value="Submit" /&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
</div>
<p>You should notice in the above code that the form is posting to a file named <code>check.php</code>.  This file will query our previously created MySQL database to verify the user&#039;s credentials.  Here is how it is coded:
<div class="code">
<pre><code>&lt;?php
  session_start();

  if($_SERVER['REQUEST_METHOD'] == "POST") {
    mysql_connect("mysql.example.com", "username", "password");
    @mysql_select_db("database") or die( "Unable to connect to database");
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $result = mysql_query("SELECT * FROM users WHERE username='$username' AND
      password=md5('$password')");

    if(mysql_num_rows($result) &gt; 0) {
      $_SESSION['is_logged_in'] = 1;
    }
  }

  if(!isset($_SESSION['is_logged_in'])) {
    header("location:login.php");
  } else {
    header("location:authenticated.php");
  }
?&gt;</code></pre>
</div>
<p>I have created a page that requires this authentication for you to view, you can visit it <a href="http://studge.com/testbed/authenticated.php">here</a>.  It is named <code>authenticated.php</code> and that is why the <code>else</code> statement above references that page.  It will redirect you to the <code>login.php</code> page unless you have been authenticated.  The username and password are <code>user</code> and <code>password</code> respectively.  On the authenticated test page you are given to option of logging out.  This was done by creating a file named <code>logout.php</code> with the following code:
<div class="code">
<pre><code>&lt;?php
  session_start();
  session_destroy();

  header("location:authenticated.php");
?&gt;</code></pre>
</div>
<p>That&#039;s all there is to it.  If you have any questions you can leave a comment or use the <a href="http://studge.com/contact/">contact</a> page and I will get back to you as soon as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/create-a-site-authentication-login-with-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Displaying Click Counts with PHP and MySQL</title>
		<link>http://studge.com/displaying-click-counts-with-php-and-mysql/</link>
		<comments>http://studge.com/displaying-click-counts-with-php-and-mysql/#comments</comments>
		<pubDate>Sat, 19 May 2007 17:01:34 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[(X)HTML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://studge.com/displaying-click-counts-with-php-and-mysql/</guid>
		<description><![CDATA[Terry at SuperAff.com has asked about a follow up to a previous post Hiding Links With PHP and Counting Clicks with MySQL. In this article I demonstrated how to hide links (affiliate or otherwise) and count how many times each was clicked.  Here I will show how to throw together a simple MySQL query [...]]]></description>
			<content:encoded><![CDATA[<p>Terry at <a href="http://superaff.com/archives/2007/05/18/how-to-cloak-affiliate-links-click-tracking-with-php/">SuperAff.com</a> has asked about a follow up to a previous post <a href="http://studge.com/hiding-links-with-php-and-counting-clicks-with-mysql/">Hiding Links With PHP and Counting Clicks with MySQL</a>. In this article I demonstrated how to hide links<span id="more-50"></span> (affiliate or otherwise) and count how many times each was clicked.  Here I will show how to throw together a simple MySQL query to show you the click count.</p>
<p>First, you need to follow the aforementioned post to set up the click counter.  In the interest of not repeating myself, I will omit that code from this post.  Following is the PHP code that we will use:
<div class="code">
<pre><code >&lt;?php
  mysql_connect("mysql.example.com", "username", "password");
  @mysql_select_db("database") or die( "Unable to connect to database");
  $result = mysql_query("SELECT * FROM linkcount");

  $num = mysql_num_rows($result);

  mysql_close();

  echo "&lt;table&gt;&lt;tr&gt;&lt;td&gt;Link&lt;/td&gt;&lt;td&gt;Count&lt;/td&gt;&lt;/tr&gt;";

  for ($i=0; $i&lt;$num; $i++) {
	  if ($tmp = mysql_fetch_array($result)) {
		  extract($tmp);

		  echo "&lt;tr&gt;&lt;td&gt;$id&lt;/td&gt;";
		  echo "&lt;td&gt;$count&lt;/td&gt;&lt;/tr&gt;";
	  }
  }

  echo "&lt;/table&gt;";
?&gt;</code></pre>
</div>
<p>There are many different ways to code a front-end like this.  I am by no means a PHP expert, but I know enough to usually get the job done.  This code will give you a good starting point and you can elaborate on the code with some simple HTML to dress up the display table a bit.  I would usually advise against using tables in your web development ventures, but when it comes to returning MySQL queries there really isn&#039;t a better way.</p>
<p>To display these results in a Wordpress post, you will need a plugin to allow running custom PHP code.  Wordpress does not allow the execution of PHP code by default.  In this blog I only show the code and do not have a real need to embed it &#8211; so I do not currently use one of these plugins.  However, you can view the results of the above code <a href="http://studge.com/testbed/display-clicks.php">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/displaying-click-counts-with-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>JavaScript Validation for HTML Forms</title>
		<link>http://studge.com/javascript-validation-for-html-forms/</link>
		<comments>http://studge.com/javascript-validation-for-html-forms/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 13:07:19 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[(X)HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://studge.com/javascript-validation-for-html-forms/</guid>
		<description><![CDATA[One of the most frustrating things with using forms on a site is getting back bad information in your results. Probably the most common instance is when a user enters a bad email address, such as webmaster#studgecom.  We can utilize JavaScript to check for composition error such as this and to make sure that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most frustrating things with using forms on a site is getting back bad information in your results.<span id="more-35"></span> Probably the most common instance is when a user enters a bad email address, such as <code>webmaster#studgecom</code>.  We can utilize JavaScript to check for composition error such as this and to make sure that certain, required fields are filled in.</p>
<p>First, we are going to write up a simple form that requests a user&#039;s name, email address and favorite color.  Here is the essence of the form:</p>
<div class="code">
<pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Form for JavaScript Validation - 1st version&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;form method="POST" action="jstest.php"&gt;
    	Name:&lt;input type="text" name="name" /&gt;&lt;br /&gt;
	Email address:&lt;input type="text" name="email" /&gt;&lt;br /&gt;
	Favorite color:&lt;input type="text" name="color" /&gt;&lt;br /&gt;
	&lt;input type="submit" name="submit" value="Submit" /&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
</div>
<p>You can try it <a href="http://studge.com/testbed/jstest1.html" target="_blank">here</a>. It sends the form data to a PHP script named <code>jstest.php</code>.  This script merely displays the information that the user put into the form.  Here is code for it:
<div class="code">
<pre><code>&lt;?php
  echo "Thank you <strong>" . $_POST['name'] . "</strong>&lt;br /&gt;";
  echo "We will contact you at <strong>" . $_POST['email'] .
    "</strong> regarding the color <strong>" . $_POST['color'] . "</strong>";
?&gt;</code></pre>
</div>
<p>As it is written now, the form provides no sort of validation and the user can submit anything or nothing in the availabe fields.  So we need to write a JavaScript script to handle the validation.  We will <strong>require</strong> the user to input their name, email address and favorite color.  The user must also input the email address in a valid format. We will call this file <code>jstest.js</code> and here is what it looks like:
<div class="code">
<pre><code>function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos&lt;1||dotpos-apos&lt;2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_form(thisform) {
	with (thisform) {
		if (validate_required(name,"Please specify your name!")==false) {
			name.focus();
			return false;
		}
		if (validate_required(email,"Email must be filled out!")==false) {
			email.focus();
			return false;
		}
		if (validate_email(email,"Not a valid e-mail address!")==false) {
			email.focus();
			return false;
		}
		if (validate_required(color,"Forgot favorite color!")==false) {
			color.focus();
			return false;
		}
	}
}</code></pre>
</div>
<p>Now we have a script to validate the form, but it does nothing until we implement it into the form&#039;s code.  Here is the full form code (sans styling):
<div class="code">
<pre><code>&lt;html&gt;
&lt;head&gt;
  &lt;script type="text/javascript" src="jstest.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;form method="POST"
        onsubmit="return validate_form(this)" action="jstest.php"&gt;
    Name:&lt;input type="text" name="name" /&gt;&lt;br /&gt;
    Email address:&lt;input type="text" name="email" /&gt;&lt;br /&gt;
    Favorite color:&lt;input type="text" name="color" /&gt;&lt;br /&gt;
    &lt;input type="submit" name="Submit" value="Submit" /&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
</div>
<p>You can try the finished, with validation, <a href="http://studge.com/testbed/jstest2.html" target="_blank">here</a>.  You should be able to adapt the above code to your specific form needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/javascript-validation-for-html-forms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Hiding Links With PHP and Counting Clicks with MySQL</title>
		<link>http://studge.com/hiding-links-with-php-and-counting-clicks-with-mysql/</link>
		<comments>http://studge.com/hiding-links-with-php-and-counting-clicks-with-mysql/#comments</comments>
		<pubDate>Sat, 07 Apr 2007 17:44:57 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[(X)HTML]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://studge.com/hiding-links-with-php-and-counting-clicks-with-mysql/</guid>
		<description><![CDATA[There are many reasons why one might want to hide their links &#8211; perhaps you are are an affiliate marketer, the link is really long and dirty or you are simply wanting to implement a system that counts how many times a certain link is clicked (more on that later).
Affiliate marketers like to make their [...]]]></description>
			<content:encoded><![CDATA[<p>There are many reasons why one might want to hide their links &#8211; perhaps<span id="more-38"></span> you are are an affiliate marketer, the link is really long and dirty or you are simply wanting to implement a system that counts how many times a certain link is clicked (more on that later).</p>
<p>Affiliate marketers like to make their links appear to be on their site and handle the redirection themselves, it improves the overall trustworthiness and cleanliness of the site in general.  In keeping with the <a href="http://en.wikipedia.org/wiki/Clean_URLs#Clean_URLs">clean URLs</a> process, though I&#8217;m not sure if this really counts, you may want to clean up a link for whatever reason.  The last reason I can think of, and the most useful, is that you what to implement a system to manage your links and count them at the same time.  This can be a valuable tool in any online marketing campaign as you try to feel out what works on your sites.  First off, we are going to need a PHP script to send all of our link requests to.  For this example, we will create a script at the root of our domain called <code>redirect.php</code>.  The following is the first bit of code that will handle the actual redirection for the links, for our example it will redirect to Google, Yahoo and Wikipedia:</p>
<div class="code">
<pre><code >&lt;?php
  $id = $_GET['id'];
  $links = array(
    "google" =&gt; "http://google.com",
    "yahoo" =&gt; "http://yahoo.com",
    "wikipedia" =&gt; "http://wikipedia.org"
    );

  header("Location:".$links[$id]);
  exit;
?&gt;</code></pre>
</div>
<p>Now we can use the following links to get to our specified locations:
<ul>
<li><a href="http://studge.com/testbed/redirect.php?id=google">Google</a> <code>http://studge.com/testbed/redirect.php?id=google</code></li>
<li><a href="http://studge.com/testbed/redirect.php?id=yahoo">Yahoo!</a> <code>http://studge.com/testbed/redirect.php?id=yahoo</code></li>
<li><a href="http://studge.com/testbed/redirect.php?id=wikipedia">Wikipedia</a> <code>http://studge.com/testbed/redirect.php?id=wikipedia</code></li>
</ul>
<p>Now we can also set up a system to count how many clicks our new links are getting.  I am going to use a MySQL database for this feature.  I will need to set up the following table:</p>
<div class="code">
<pre><code>CREATE TABLE `linkcount` (
`id` VARCHAR( 50 ) NOT NULL ,
`count` INT( 10 ) NOT NULL default '0' ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;</code></pre>
</div>
<p>We are going to edit the redirection code to access the database and create an entry for the link if it does not already exist.  Then we will increment the count field by one each time it is called. This example uses the fictional database server <code>mysql.example.com</code>, using a database called <code>database</code> with a table called <code>linkcount</code>:
<div class="code">
<pre><code>&lt;?php
  mysql_connect("mysql.example.com", "username", "password");
  @mysql_select_db("database") or die( "Unable to connect to database");
  $id = mysql_real_escape_string($_GET['id']);
  $result = mysql_query("SELECT * FROM linkcount WHERE id='$id'");

  if(mysql_num_rows($result) == 0) {
  	mysql_query("INSERT INTO linkcount (id) VALUES ('$id')")
          or die(mysql_error());
	mysql_query("UPDATE linkcount SET count=count+1 WHERE id='$id'")
          or die(mysql_error());
  } else {
	mysql_query("UPDATE linkcount SET count=count+1 WHERE id='$id'")
          or die(mysql_error());
  }

  mysql_close();

  $links = array(
    "google" =&gt; "http://google.com",
    "yahoo" =&gt; "http://yahoo.com",
    "wikipedia" =&gt; "http://wikipedia.org"
    );

  header("Location:".$links[$id]);
  exit;
?&gt;</code></pre>
</div>
<p>That is all it takes.  From here you may want to write a front-end to display your click results or you could even display the count next to the specific link in your sites navigation menu to rank the most popular outgoing links.</p>
<p><strong>Edit:</strong> A follow-up has been written to this post that describes a simple script for displaying the click results: <a href="http://studge.com/displaying-click-counts-with-php-and-mysql/">Displaying Click Counts with PHP and MySQL</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/hiding-links-with-php-and-counting-clicks-with-mysql/feed/</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.139 seconds -->
