Home arrow MySQL arrow Page 5 - Building a Simple Affiliate System in PHP/MySQL

Letting Affiliates Log in - MySQL

Setting up an affiliate system on your website can be very lucrative. While there are a lot of details involved, it need not be overly complicated. This article walks you through the steps of creating and setting up a basic affiliate system.

TABLE OF CONTENTS:
  1. Building a Simple Affiliate System in PHP/MySQL
  2. Building The Database
  3. Being Common
  4. Your Index Page
  5. Letting Affiliates Log in
  6. Adding Affiliate Code to Your Site
By: Roger Stringer
Rating: starstarstarstarstar / 58
November 10, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Okay, affiliates are able to sign up. Now, we need to let them log in to their accounts. We'll now create two files for that.

The first file is called "login.php":

<?php
  session_start();
  $auid = isset($_POST['auid']) ? $_POST['auid'] : $_SESSION['auid'];
  $pwd = isset($_POST['pwd']) ? $_POST['pwd'] : $_SESSION['pwd'];

  if(!isset($auid)) {
    loginform();
    exit;
  }
  $results = mysql_query("SELECT * FROM affiliates WHERE username='{$auid}' && password='{$pwd}'");
  $row = mysql_fetch_assoc($results);
  if($row['username'] == $auid && $row['password'] == $pwd){
    $_SESSION['userId'] = $row['ID'];
    $_SESSION['username'] = $row['username'];
    $_SESSION['email'] = $row['email'];
    $_SESSION['akey'] = $row['akey'];
  }else{
    session_destroy();
    session_start();
    loginform();
    exit;
  }
?>
<?
  function loginform(){
    global $sitename;
?>
    <CENTER>
    <BR>
    <FONT FACE=verdana color=#FFFFFF size=5><B><?=$sitename?> Login</B></FONT>
    <BR>
    <TABLE cellspacing=0>
    <FORM method=post>
    <TR><TD>Username:
      <TD><INPUT type=text name=auid size=16>
    <TR><TD>Pass:
      <TD><INPUT type=pass name=pwd size=16>
    <TR><TD colspan=2><INPUT type=submit value="Log In >>"></TD>
    </FORM>
    </TABLE>
    <BR>
<?
  }
?>

This file is used to let your affiliates log in to the site.

Next, we create a file called "account.php":

<?
  include("common.php");
  include("login.php");
  include("header.php");
  $mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
?>
 <p>Welcome <?=$_SESSION['username']?><br/>
  <hr/>
    <a href='account.php'>Your Account</a>
    &nbsp;|&nbsp;
<a href='account.php?mode=affedit'>Edit Your Profile</a>
<br/>
  <hr/></p>
<?
  if($mode == 'affedit'){
    include("affedit.php");
  }else{
    include("affstats.php");
  }
  include("footer.php");
?>

This is the main file that your affiliates will use for viewing their stats and updating their account.

We now will create two more files as part of this section.

The first one is "affstats.php." This file displays the affiliate stats.

<p>Your Affiliate Link:<br/>
<textarea rows=3 cols=50 wrap=SOFT><?=$siteurl?>/click.php?refid=<?=$_SESSION['akey']?></textarea>
<hr/>
<?
$mystats = AffiliateStats($_SESSION['userId']);
if($mystats['clicks'] > 0){
  $ratio = number_format( (($mystats['orders'] / $mystats['clicks']) * 100), 2 )." %";
}else{
  $ratio = "0 %";
}
?>
<b>Your Stats</b><br/></p>
<table align=center width=350 border=1>
<tr>
  <td>Total Clicks:</td>
  <td><?=number_format($mystats['clicks'],0)?>&nbsp;</td>
</tr>
<tr>
  <td>Total Orders:</td>
  <td><?=number_format($mystats['orders'],0)?>&nbsp;</td>
</tr>
<tr>
  <td>Ratio %:</td>
  <td><?=$ratio?>&nbsp;</td>
</tr>
</table>
<br>
<table align=center width=350 border=1>
<tr>
  <td>Amount Earned:</td>
  <td>$<?=number_format($mystats['order_total'],2)?></td>
</tr>
<tr>
  <td>Payments Pending:</td>
  <td>$<?=number_format($mystats['pending_total'],2)?></td>
</tr>
<tr>
  <td>Amount Paid:</td>
  <td>$<?=number_format($mystats['paid_total'],2)?></td>
</tr>
</table>
<hr/>

Next, create a file called "affedit.php." This file lets your affiliates edit their accounts.

<?
  $step = isset($_POST['step']) ? $_POST['step'] : 1;
  switch ($step){
    case '2':
      AffiliateUpdate($_SESSION['userId']);
      echo "Your account has been updated<br>";
      break;
    default:
      $affinfo = mysql_fetch_assoc( mysql_query("SELECT * FROM affiliates WHERE ID='{$_SESSION['userId']}'") );
?>
      <table>
      <form method="POST" action="account.php?mode=affedit">
      <input type='hidden' name='step' value='2'>
<?
      foreach($affiliateForm as $field=>$info){
        echo "<tr>";
        if( !empty($info[0]) ){
          echo "<td valign=top>{$info[0]}</td>";
        }
        echo "<td valign=top>";
        switch($info[1]){
          case "hidden":
            // hidden variables don't get edited.
            break;
          case "password":
            echo "<input type='password' name='{$field}' value='$affinfo[$field]'>";
            break;
          case "text":
            echo "<input type='text' name='{$field}' value='{$affinfo[$field]}'>";
            break;
          case "textarea":
            echo "<textarea name='{$field}' rows=5 cols=25>{$affinfo[$field]}</textarea>";
            break;
          case "select":
            $info[2] = explode("|",$info[2]);
            echo "<select name='{$field}'>";
            foreach($info[2] as $k=>$v){
              if($v == $affinfo[$field]){
                echo "<option SELECTED>{$v}</option>";
              }else{
                echo "<option>{$v}</option>";
              }
            }
            echo "</select>";
            break;
        }
        echo "</td>";
        echo "</tr>";
      }
?>
      <tr>
        <td colspan=2><input type='submit' value='Save'></td>
      </tr>
      </form>
      </table>
<?
      break; 
  }
?>

Okay, this is it for the part of the script that handles affiliates managing their accounts. Now, we need to a way to manage affiliates.



 
 
>>> More MySQL Articles          >>> More By Roger Stringer
 

blog comments powered by Disqus
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: