MySQL
  Home arrow MySQL arrow Page 5 - Building a Simple Affiliate System in ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Building a Simple Affiliate System in PHP/MySQL
By: Roger Stringer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 41
    2005-11-10

    Table of Contents:
  • Building a Simple Affiliate System in PHP/MySQL
  • Building The Database
  • Being Common
  • Your Index Page
  • Letting Affiliates Log in
  • Adding Affiliate Code to Your Site

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Simple Affiliate System in PHP/MySQL - Letting Affiliates Log in


    (Page 5 of 6 )

    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


       · When I saw the headline, I was quite anxious to read this article. Unfortunately,...
       · We are sorry to hear that you found the article disappointing. I am contacting the...
       · Well, The article is about building a simple affiliate system. Which is what it...
       · Hi. DevShed is all about open source programming. This article explores the...
       · This article was entirely appropriate. I mean if it was about inventory tracking...
       · Hello All, A big thanks to the author for taking time to write this...
       · There was a slight bad there when they inserted the article into the system...
       · Thanks! I'll let ya know how I make out!MrsDelley
       · and what is this:Parse error: parse error, unexpected ';' in...
       · sorry i am blind today, although it is an error.for those who may ask this Q...
       · one more Qurestion i have; What is this: <img src="/affiliates/sale.php">....
       · I keep getting this error message:Warning: mysql_query(): supplied argument is...
       · hi, first of all thanks for sharing this nice script. it seems to do exactly what i...
       · scrap my last comment, i should be logging into manage.php not accesscotrol.php....
       · Its a very nice script.:)Some notes from the comments that could be updated to...
       · I get the same exact error message. Could someone shed some light on the solution...
       · I get alot of these errors:Warning: mysql_query(): supplied argument is not a...
       · Hi There,this script in common.php:list($pending_total) = mysql_fetch_row(...
       · I have gone through each page and created all the files. Needless to say, there is a...
       · You access your MySQL database through a program called PHPMyAdmin. It should be...
       · Hi to the Poster above,I did eventually figure out how to upload the tables as...
     

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway