<?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; MySQL</title>
	<atom:link href="http://studge.com/category/mysql/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>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>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>
		<item>
		<title>Setting up MySQL in Fedora Core 6</title>
		<link>http://studge.com/setting-up-mysql-in-fedora-core-6/</link>
		<comments>http://studge.com/setting-up-mysql-in-fedora-core-6/#comments</comments>
		<pubDate>Mon, 12 Mar 2007 04:41:09 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://studge.com/setting-up-mysql-in-fedora-core-6/</guid>
		<description><![CDATA[I decided to develop a web portal that runs off of my laptop for my personal use.  Since it is the only computer I use for business, it seemed like the logical direction to take.  One problem that I have always had when developing a server setup is getting MySQL to do what [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to develop a web portal that runs off of my laptop for my personal use.  Since it is the only computer I use for business, it seemed like the logical direction to take.  One problem that I have always had when developing a server setup is getting MySQL to do what it is supposed to do.<span id="more-28"></span> Here is my personal documentation so hopefully I will not have to suffer with it again. This post is about Fedora Core, so I will include the <a href="http://linux.duke.edu/projects/yum/">yum</a> statements I used.  For other distros, just install the equivalent packages.</p>
<div class="code">
<pre><code>[root@localhost ~]# yum install mysql mysql-devel mysql-server</code></pre>
</div>
<p>Now, I am going to start the MySQL server and set it up to start everytime I boot.</p>
<div class="code">
<pre><code>[root@localhost ~]# service mysqld start
Starting MySQL:     [ OK ]
[root@localhost ~]# chkconfig mysqld on</code></pre>
</div>
<p>Now I need to set the password for the root user:</p>
<div class="code">
<pre><code>[root@localhost ~]# mysqladmin -u root password <em>yourpassword</em></code></pre>
</div>
<p>Since I am going to install <a href="http://drupal.org/">Drupal</a> as a CMS for my personal portal, I am going to need a database for the installation.  I will create a database named &#8216;drupal&#8217; and then create a user account for this database called &#8216;duser&#8217;.  It is not recommended that you use the root account for accessing your MySQL databases.</p>
<div class="code">
<pre><code>[root@localhost ~]# mysql -u root -p
Enter password:
mysql&gt;; CREATE DATABASE drupal;
Query OK, 1 row affected (0.09 sec)
mysql&gt; GRANT ALL PRIVILEGES ON drupal.*
  -&gt; TO 'duser'@'localhost' IDENTIFIED BY
  -&gt;'<em>yourpassword</em>' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
mysql&gt; quit
Bye</code></pre>
</div>
<p>That&#8217;s all there is to it.  After that, I put the access information that I just created into Drupal&#8217;s database configuration page and it installed the CMS for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/setting-up-mysql-in-fedora-core-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

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