IPB

Welcome Guest ( Log In | Register )


Welcome Guest, Register to Remove this Message!
 
Reply to this topicStart new topic
> [Intermediate] Store buddies who click profile, Whoever clicks x=%n is added to database
Timo
post Feb 1 2004, 08:58 PM
Post #1


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



This tutorial shows you how to copy the "imchaos like" buddy name storing from your aol profile. You could use it to create your own subjournal and store buddy's names or just to keep a paranoid account of who clicks your site and has you in their profiles. (Even I do it. :-X)

In order for this tutorial to work you need a MySQL database and PHP w/ access to Get variables... (Sometimes it's switched off by free hosts because it can lead to security holes if the users don't watch their code, but the majority leave it open.)

Here is the structure of the table...

You should, by use of Leo's tutorial, be easily able to just SQL dump this into phpmyadmin
CODE

#
# Table structure for table `VisLog`
#

DROP TABLE IF EXISTS `VisLog`;
CREATE TABLE `VisLog` (
 `ID` mediumint(9) NOT NULL auto_increment,
 `VisNums` mediumint(9) NOT NULL default '0',
 `intp` mediumtext NOT NULL,
 `btype` mediumtext NOT NULL,
 `VisName` mediumtext NOT NULL,
 `date` date NOT NULL default '0000-00-00',
 KEY `ID` (`ID`)
) TYPE=MyISAM AUTO_INCREMENT=1;


Add this function to the top of your page...
Why? It strips away characters which could allow a malicious hacker to manipulate your sql query. AIM doesn't use these characters anyway, so there won't be much of an issue out of it. (While many script kiddies probably couldn't get into your site and it would take a person with knowledge of MySQL and usually some sort of idea of how your database is setup, it's a good idea to close any holes.)
CODE

<?php
function dbinsans($text) {
$text = strip_tags ($text, "");
$text = str_replace(chr(10),"",$text);
$text = str_replace(chr(13), "<br>", $text);
$text = str_replace("\"","*",$text);
$text = str_replace("'","*",$text);
$text = str_replace(" ","",$text); #added for name formatting
$text = addslashes($text);
return($text);
}
?>

The piece checks the variable and removes the possible security holes...
When you load up the link to add the user names right now it's set to
www.yoursite.com/yourfolder/yourphpfilethisisin.php?x=name
if you wanted to change it to, let's say, y so it would work like
www.yoursite.com/yourfolder/yourphpfilethisisin.php?y=name
change the
$x = strtolower(dbInsans($_GET['x']));
to
$x = strtolower(dbInsans($_GET['y']));
CODE

<?php
$x = strtolower(dbInsans($_GET['x'])); #Sets the get variable named x to the php variable $x

?>

Be sure to either include a file with your db variables, like most people including myself do
or set them here.
CODE

<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>

I don't know if those are conventional names, but a lot of people tend to use those,
if you don't use the same variable names feel free to alter my code below to match yours.

Now for the code to add the name or update the count,
place this anywhere under the function code...
You can even place them right after each other.
CODE

<?php
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);

$requete = "SELECT * FROM VisLog WHERE VisName='$x'";
#A query to determine if the name the user used is in the database

$ipaddy = $_SERVER["REMOTE_ADDR"];
#Gives the variable the ip address

$bstring =$_SERVER["HTTP_USER_AGENT"];
#Gives the variable the browser info

$result = mysql_query ($requete,$db);
#Should return only one value, our guy if he exists

$article = mysql_fetch_object($result);
#Gives an object with our row back

if ($article->VisName==$x)
 {
 $visits=$article->VisNums+1;
 #First finds the old visits, then adds one and sets it

$sql = "UPDATE VisLog  SET VisNums='$visits', intp='$ipaddy', btype='$bstring', date=curdate() WHERE VisName='$x'";
 #Updates ip, browser info, and visit count, and date

 mysql_query($sql, $db);
 #Queries the database and updates the user.

 }
else if ($x!=NULL) #Checks to see if there is anything in x, if not, don't add
{
$sql = "INSERT INTO VisLog (VisNums, VisName, intp, btype, date) VALUES (1, '$x', '$ipaddy', '$bstring', curdate())";
 #Creates the insert query string.

 mysql_query($sql, $db);
 #Queries the database and adds the user.

}
mysql_free_result($result);
?>



That's all the code you need to add people to the database, but how about view who's been to your site?
Well here's the code I came up with... I highly suggest you place this in a password protected directory, or at least in an admin section.
A real "friend" doesn't place his friend's ip addresses and screen names for the public eye. ;-)
(Remember to either include your admin variables file or include...)
CODE

<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>

(again)
CODE

<?php
$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);
$requete = "SELECT * FROM VisLog";
$result = mysql_query ($requete,$db);
while ($article = mysql_fetch_object($result))
{
echo $article->VisName." has visited ".$article->VisNums." times...<br />";
echo "Last IP = <a href=http://ws.arin.net//cgi-bin/whois.pl?queryinput=".$article->intp.">".$article->intp."</a><br />// User Browser info...".$article->btype."<br /><br />";

}
mysql_free_result($result);
?>


Feel free to alter the code above into a table or whatever formatting you desire.
(You'll need some basic knowledge about echo statements though... not really that hard.)

Finally, when you want to create a link in your AIM profile set the link like this...
www.yoursite.com/yourfolder/thepage.php?x=%n


I hope this helped out some people. laugh.gif I find it handy.

-Timo


Edited: 2/8/04; now includes date so it will tell when the last time the person visited your page was.
Edited: 2/11/04; now works with name formatting to remove spaces and make lowercase; it can now catch when people change their name format.

This post has been edited by Timo: Feb 11 2004, 06:49 PM


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
leo
post Feb 1 2004, 09:04 PM
Post #2


Professional newbie
Group Icon

Group: Admin
Posts: 7238
Joined: 8-October 03
From: in the backroom counting up your visitors
Member No.: 4



astounding tutorial timo!! biggrin.gif

Gotta give it a go cool.gif


/added

oops i see the quick reply box isn't enabled in the tutorial category, better go enable it smile.gif


--------------------
Get Tough - Take photoshop by the horns - photoshop tutorials
whatcounter.com - free hit counter - quality stats about your website rivalled by nothing else.
TinyShortURL - Free URL shortening service
myIp.weborum.com - What's my IP ?
sms.weborum.com - Free US sms messages for Weborum members
Go to the top of the page
 
+Quote Post
Timo
post Feb 1 2004, 09:09 PM
Post #3


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



Haha thanks,
I think I made this about a year ago when I was first starting out with php. I just, about 10 mins ago, put the tutorial code together and tried to optimize it. I tend to be dyslexic at times though, so give me a holler if it causes an error. I think it'll work well though still.

Oh here's a little addition...

CODE


<?php

$db = mysql_connect("$dbHost","$dbUser","$dbPass");
mysql_select_db($dbname,$db);

$z = $_GET['z'];
#You don't need to strip anything out of this, we're not using it in a query.
#It's only being used to compare.
echo 'View by... <br />';
if ($z==NULL||$z==1||$z>7)
echo '[First Inserted First]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=1">First Inserted First</a>]';
if ($z==2)
echo '[Alphabetical Order]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=2">Alphabetical Order</a>]';
if ($z==3)
echo '[Reverse Alphabetical Order]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=3">Reverse Alphabetical Order</a>]';
if ($z==4)
echo '[Least Visits First]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=4">Least Visits First</a>]';
if ($z==5)
echo '[Most Visits First]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=5">Most Visits First</a>]';
if ($z==6)
echo '[Earliest Visits First]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=6">Earliest Visits First</a>]';
if ($z==7)
echo '[Latest Visits First]';
else
echo '[<a href="'.$_SERVER['SCRIPT_NAME'].'?z=7">Latest Visits First</a>]';
echo '<br /><br />';
if ($z==1) #order inside the database
$requete = "SELECT * FROM VisLog";
else if ($z==2)
$requete = "SELECT * FROM VisLog ORDER By VisName Asc";  #ABC Names
else if ($z==3)
$requete = "SELECT * FROM VisLog ORDER By VisName Desc"; #ZYX Names
else if ($z==4)
$requete = "SELECT * FROM VisLog ORDER By VisNums Asc"; #Who visits the lowest to highest
else if ($z==5)
$requete = "SELECT * FROM VisLog ORDER By VisNums Desc"; #highest to lowest
else if ($z==6)
$requete = "SELECT * FROM VisLog ORDER By date Asc"; #Who visits earliest on
else if ($z==7)
$requete = "SELECT * FROM VisLog ORDER By date Desc"; #visited last
else
$requete = "SELECT * FROM VisLog"; #Normal like $z==1
echo '<table border="1" style="width:100%; background-color:#cfc" cellspacing="0" cellpadding="0"><tr><td>Name</td><td>Visit #s</td><td>Date</td><td>IP Address</td><td>Browser Info</td></tr>';
echo '<tr><td style="background-color:#ffc">&nbsp;</td><td style="background-color:#ffc">&nbsp;</td><td style="background-color:#ffc">&nbsp;</td><td style="background-color:#ffc">&nbsp;</td><td style="background-color:#ffc">&nbsp;</td></tr>';
$result = mysql_query ($requete,$db);
$r=0;
while ($article = mysql_fetch_object($result))
{
if ($r==0) {
echo '<tr><td>'.$article->VisName.'</td><td>'.$article->VisNums.'</td><td>'.$article->date.'</td>';
echo '<td><a href="http://ws.arin.net//cgi-bin/whois.pl?queryinput='.$article->intp.'">'.$article->intp.'</a></td><td>'.$article->btype.'</td></tr>';
$r=1;
}else
{
echo '<tr><td style="background-color:#ffc">'.$article->VisName.'</td><td style="background-color:#ffc">'.$article->VisNums.'</td><td style="background-color:#ffc">'.$article->date.'</td>';
echo '<td style="background-color:#ffc"><a href="http://ws.arin.net//cgi-bin/whois.pl?queryinput='.$article->intp.'">'.$article->intp.'</a></td><td style="background-color:#ffc">'.$article->btype.'</td></tr>';
$r=0;
}
}
echo '</table>';
mysql_free_result($result);
?>


It wouldn't be too hard to add a date field to this too, but I never did in my original script and really had no use with knowing when the people came, I just was more into how many times.

With the above code open the file with the view people with whichever sorting you want... You could even put the choices at the top of the page and just load like
www.yoursite.com/yourfolder/adminscript.php?z=1

1. Shows it from who came first to last, the order they were inserted
2. Shows by ABC names
3. Shows by ZYX names
4. Shows by least visits
5. Shows by most visits
6. Shows by earliest date
7. Shows by latest date
Anything else or no variable. Shows who came first to last, like #1

Oh that replaces the last block of code if anyone wants to customize it. Not hard to do on your own, just thought I'd add it if people are lazy. (Like me usually.) laugh.gif

-Timo

Edited: 2/8/04; Includes the date queries and menubar.

This post has been edited by Timo: Feb 8 2004, 10:55 PM


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post
Timo
post Feb 8 2004, 10:05 PM
Post #4


I'm not drunk! ;D
******

Group: Members
Posts: 1682
Joined: 31-January 04
From: Bentley College
Member No.: 7



Two updates... They are in red up above, but here they are again.
Added a date field to the database and allowed it to automatically place the value for you of when the person visited last.

Bigger update on my second post with the update to the query.
Created a very simple text menu bar and also allowed a date query... You don't need to remember the z values now, it has it so you can just click the link.


Oh and I tested both updated scripts and they work well.

-Timo


Another addition to it.. hah I made some pretty tables to hold the info. ph34r.gif

This post has been edited by Timo: Feb 8 2004, 10:56 PM


--------------------
IPB Image, yeah, that's the name of the game.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 



Lo-Fi Version
Euribor
Reviews
Time is now: 9th September 2010 - 08:55 PM