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>
|
<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)?> </td>
</tr>
<tr>
<td>Total Orders:</td>
<td><?=number_format($mystats['orders'],0)?> </td>
</tr>
<tr>
<td>Ratio %:</td>
<td><?=$ratio?> </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.
| | Discuss Building a Simple Affiliate System in PHP/MySQL | | | | | | | 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... | | | | | | Hi, I'm have 2 problems at the moment with the system, although I see it will be... | | | | | | These scripts are broken and make a mess. | | | | | | >>> Post your comment now! | | | | | |
|
 |