PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Button affiliate system in PHP/MYSQL

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Tue Aug 31, 2010 04:25:32
Edit Post|Quote
This tutorial will teach you how to build a simple button links system and keep track of the number of visitors you send to your affiliates.
This tutorial will require PHP AND MYSQL so please be sure to have those. This tutorial will be fairly simple, we will need the main file folder and an admin folder that will be password protected. So create an admin folder that is password protected. Most control panels have the ability to do this. If yo do not have a control panel go to http://www.javascriptkit.com/howto/htaccess3.shtml to get the .htaccess to password protect directories.
First we need to create the mySQL table for this script. We will call this table affiliates_table with the following fields:

affid - bigint, primary, auto-increment
url - varchar length 255
image - varchar length 255
hitsout - bigint

Now that we've created the database table we will start off with the files that need to be in the admin folder(the password protected folder).
We will need just 5 files here:


connect.php -- The mysql Connection file
addlink.php -- add a link button
modbutton.php -- lists all the current buttons to modify and delete
editbutton.php -- Edit a button link
deletebutton.php - Delete the button

For easy navigation, please put this at the top of all files in the admin folder:

Code:

<A href='addlink.php'>Add a link</a> - <A href='modbutton.php'>Edit/delete buttons</a>


These are just simple hyperlinks.

Now we'll move onto connect.php, the connection file:

Code:

<?php
$db = mysql_connect("localhost", "username", "password") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("database_name",$db))
die("No database selected.");
?>


This is a simple connector file, put your mySQL username,password, and database name where indicated, we will want to include this file in at the top of all pages.

Now we will go to addlink.php

Code:

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$url=$_POST['url'];
$button=$_POST['button'];
if(strlen($url)<1)
{
print "You did not enter a URL.";
}
else if(strlen($button)<1)
{
print "You did not enter a button.";
}
else
{
$insertbutton="INSERT into affiliates_table (url, image) values('$url','$button')";
mysql_query($insertbutton) or die(mysql_error());
print "Button added into system.";
}

}
else
{
print "<form action='addlink.php' method='post'>";
print "URL(include http://):<br>";
print "<input type='text' name='url' size='20'><br>";
print "Image(button URL):<br>";
print "<input type='text' name='button' size='20'><br>";
print "<input type='submit' name='submit' value='submit'></form>";

}
?>

As you can see, this code has two cases, a case if it detects if the submit button has been pushed as indicated by $_POST['submit'], and an else case where it just prints a form for you to input a button. The form has two fields, the url field and the button url field. Both these fields are required or when you submit the form, you will get an error message telling you that you either did not enter a URL or a button. After you press submit and the form detects both a url and a button url entry, it then inserts the data you entered with a mySQL INSERT command. Note in the insert statement the stuff in the first parenthesis are the names of the fields in which you created in mySQL and the stuff in the 2nd parenthesis are the variable with the data that will be filled in. If the SQL insert is successful if will print "Button added to system", otherwise it will throw a error as defined by mysql_error() .

Now lets go to modbutton.php:

Code:

<?php
include "connect.php";
$getbuttons="SELECT * from affiliates_table order by hitsout desc";
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
print "<table border='1'>";
print "<tr><td>Button link</td><td>Hits out</td><td>Edit</td><td>Delete</td></tr>";
while($getbuttons3=mysql_fetch_array($getbuttons2))
{
print "<tr><td><A href='$getbuttons3[url]' target='_blank'><img src='$getbuttons3[image]' border='0'></a></td>";
print "<td>$getbuttons3[hitsout]</td><td><A href='edit.php?ID=$getbuttons3[affid]'>Edit</a></td>";
print "<td><A href='delete.php?ID=$getbuttons3[affid]'>Delete</a></td></tr>";
}
print "</table>";
?>


This code simply selects all the button links in the database and displays them by the number hits out you've given them, with the greatest number of hits out first. It loops through the query and prints out the button link, the number of hits out the link has, and two links to edit and delete the link.

Now we will go to the edit.php page.

Code:

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$url=$_POST['url'];
$image=$_POST['image'];
$ID=$_POST['id'];
if(strlen($url)<1)
{
print "You did not enter a url.";
}
else if(strlen($image)<1)
{
print "You did not enter an image.";
}
else
{
$updatelink="Update affiliates_table set url='$url',image='$image' where affid='$ID'";
mysql_query($updatelink) or die(mysql_error());
print "Link updated.";
}

}
else
{
$ID=$_GET['ID']; //gets the id from URL
$getbutton="SELECT * from affiliates_table where affid='$ID'";
$getbutton2=mysql_query($getbutton) or die("Could not get button");
$getbutton3=mysql_fetch_array($getbutton2);
print "<form action='edit.php' method='post'>";
print "<input type='hidden' name='id' value='$ID'>";
print "URL:<br>";
print "<input type='text' name='url' value='$getbutton3[url]' size='20'><br>";
print "Image URL:<br>";
print "<input type='text' name='image' value='$getbutton3[image]' size='20'><br>";
print "<input type='submit' name='submit' value='submit'></form>";


}
?>


This looks similar to the addlink.php page because essentially it is the same thing, except you are using the ID passed in from the URL to grab the information for the specific button you want to edit and then using that information to pre-populate the fields in the form. Once the submit button is pushed, it checks to make sure if there is any data in url and image and if there is, it updates that button record with the new information using the UPDATE query from mySQL.

Now we will go to the last page in the admin folder, delete.php

Code:

<A href='addlink.php'>Add a link</a> - <A href='modbutton.php'>Edit/delete buttons</a>

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$ID=$_POST['ID'];
$delbutton="Delete from affiliates_table where affid='$ID'";
mysql_query($delbutton) or die("Could not delete button");
print "Button deleted.";

}
else
{
$ID=$_GET['ID']; //gets the id from URL
print "<br>Are you sure you want to delete this button affiliate?<br>";
print "<form action='delete.php' method='post'>";
print "<input type='hidden' name='ID' value='$ID'>";
print "<input type='submit' name='submit' value='submit'></form>";

}
?>


This is a simple file, it uses the ID passed in and puts that ID in a hidden field, when you hit the submit button, it deletes the button entry with that ID.
Now that the administration stuff is done, we will go to the frontend and write the simple code for displaying the buttons.
So in your main folder we will need two files:

aff.php -- The page that displays the buttons
out.php -- The page that transfers counts how many times a button has been clicks and redirects to the actual site the button is pointing to.

so in aff.php:

Code:

<?php
include "admin/connect.php";
$counter=1;
$getbuttons="SELECT * from affiliates_table order by hitsout desc";
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
while($getbuttons3=mysql_fetch_array($getbuttons2))
{
if($counter%4==0)
{
print "<A href='out.php?ID=$getbuttons3[affid]'><img src='$getbuttons3[image]' border='0'></a><br>";
}
else
{
print "<A href='out.php?ID=$getbuttons3[affid]'><img src='$getbuttons3[image]' border='0'></a>&nbsp;&nbsp;";
}
$counter++;
}
?>


So this code select all the buttons in the database table and then sets a counter to see how many you've looped through, in this code example if there already been 4 buttons in a row, it wil start a new line as indicated by <br>. Now we need out.php to actually count the number of hits out and redirect to the actual URL.

Code:

<?php
include "admin/connect.php";
$ID=$_GET['ID'];
$getbuttons="SELECT url from affiliates_table where affid='$ID'";
$getbuttons2=mysql_query($getbuttons) or die("Could not get buttons");
$getbuttons3=mysql_fetch_array($getbuttons2);
$updatehits="Update affiliates_table set hitsout=hitsout+1 where affid='$ID'";
mysql_query($updatehits) or die("Could not update hits");
//time for a meta-refresh direct
print "<META HTTP-EQUIV = 'Refresh' Content = '1; URL =$getbuttons3[url]'> ";
?>


What this does is it gets the ID from the URL, then it selects just the URL of the record with that ID because we need to URL to redirect to the actual site. Then it updates the number of hits for the record with that ID and increments it by 1. Finally it does a meta redirect to the real url.

Thats it for your button system!

-----------------------------
Chipmunk,
Supreme Administrator

mrkboucher
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 24366
[PM mrkboucher]

Posted at Tue Sep 15, 2009 05:25:53
Edit post|Quote
Affiliate is system is what in which you track your current member referred some new member to join your site or purchase your product. You give some reward for it. Making or better say coding your affiliate system is easy. Benefit of own script is you can customize it as per your need.We will use cookies and MySql database to track affiliates. Why cookies not directly storing in some variable ? If we store in cookie the referrer code will stay in user browser till he/she clears cookie, but if we use PHP variable and if page changes or registration fails the affiliate won’t get counted.//here we are starting new session then we are checking whether the new comer is referred by someone else before this or not, if not then store the referrer name in session’s variable named referral, we are getting referral code as get.
-----------------------------
levitra - plastic surgery - pharmacies

cerse
Rank:acorn
Group: members
Posts: 5
IP Logged
PM ID and RPS ID: 27372
[PM cerse]

RPS score: 0
RPS challenge

Posted at Thu Feb 25, 2010 00:16:03
Edit post|Quote
It's an good idea i really love this and appreciate your work, you know time is money an a person is avaya training successful if he time timely en cash it, right now now i m doing mcse and hope a great career waits for me with a career oriented job, I hope, May IT gives you a great career and I suggest the people to get involved in this certification to serve the nation with blood and intellect.

Adler
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 29370
[PM Adler]

RPS score: 0
RPS challenge

Posted at Tue May 11, 2010 23:02:38
Edit post|Quote
Nice Experience to Read itBe frank in telling and Keep Sharing Always

alice
Rank:acorn
Group: members
Posts: 31
IP Logged
PM ID and RPS ID: 30842
[PM alice]

RPS score: 0
RPS challenge

Posted at Thu Jul 15, 2010 02:35:41
Edit post|Quote
hp certification
HP0-D07 HP0-J22 HP0-J25

ping123
Rank:acorn
Group: members
Posts: 10
IP Logged
PM ID and RPS ID: 31806
[PM ping123]

RPS score: 0
RPS challenge

Posted at Mon Aug 16, 2010 05:52:28
Edit post|Quote
Affiliate is grouping is what in which you road your underway member referred several newborn member to tie your place or acquire your product. You provide several move for it 70-431 dumps. Making or meliorate feature writing your affiliate grouping is easy. Benefit of possess playscript is you be able to make it as per your need.We be going to ingest cookies and MySql database to road affiliates. 70-431 exam Why cookies not direct storing in several uncertain ? If we accumulation in cake the referrer cipher be going to meet in individual application dirt he/she clears cookie, but if we ingest PHP uncertain and if tender changes or entrance fails the affiliate won�t achieve counted.//here we are play newborn conference then we are checking whether the newborn comer is referred by someone added before this or not, if not then accumulation the referrer name in session�s uncertain titled referral, we are effort referral cipher as get 70-431 questions.

abercrombie
Rank:acorn
Group: members
Posts: 3
IP Logged
PM ID and RPS ID: 32295
[PM abercrombie]

RPS score: 0
RPS challenge

Posted at Tue Aug 31, 2010 04:25:32
Edit post|Quote

             sweater has benn invented Abercrombie and evolved to now, abercrombie and fitch clothing
various brands will launch every season and year with different styles
and designs, in order to better meet the needs of the public. Men's
sweaters are mainly black, gray, dark blue, brown and other abercrombie and fitch earth-based colors, while women`s sweaters have fitch clothing also pink, fruit green, orange and other warm colors fitch clothing on the Abercrombie & fitch
basis of the and fitch clothing colors mentioned above. Abercrombie
& fitch Bright color passion sucess, it is somewhat abercrombie
and fitch clothing injecting a trace of activity in and fitch clothing dull winter.


abercrombie london


hollister clothing sale


clothes sale


mens leather thongs


Abercrombie clothing


abercrombie&fitch


men abercrombie


ambercrombie


abercrombie new


abercrombie et fitch


abercombrie fitch


abercrombie uk


Abercrombie outlet


abercrombie and fitch uk


abercrombie fitch uk


Abercrombie & fitch uk


abercrombiekids




Page: 1



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York