Forum Moderators: coopster

Message Too Old, No Replies

My first attempt at a PHP script from scratch

Majority done, need some help

         

Jeigh

11:59 am on Jun 18, 2007 (gmt 0)

10+ Year Member



I'm quite new to PHP and thought I'd have a go at making a script myself from what I've learnt.

First of all, users on my site all have a profile which is profile.php and there profiles would be profile.php?user=username. Now I'm attempting to add a bit of code that says:

Times this page has been recommended: (number from database)

Basically, I want users to be able to click a link on someones profile then it will add 1 to their total times recomnended and it will also record the users ip that recommended them, so if they try to do it again it will check for their ip and if it matches it will not let them do it. A lot of this stuff I had no idea how to do, I just assumed how to do it from what I've learnt.

When a user clicks the recommend link in someones profile they are linked to:

Code (recommend.php)

<?
include('header.php');
include('navigation.php');
include('content.php');

$username= $req_user_info['username'];

$reccount=$req_user_info['reccount'];

$ipcheck =mysql_query("SELECT recip FROM users WHERE username='$username'");

if($ip == $ipcheck)
{
echo "Sorry, you've already recommended this user";
echo "<a href=\"index.php\">Back to Index</a>";

include('endcontent.php');
include('advert.php');
include('footer.php');
}
else
{

$rec = $req_user_info['reccount'];
$addrec=$rec+1;
$recq="UPDATE users SET reccount='$addrec' WHERE username='$username'";
mysql_query($recq);

$addip="INSERT INTO users('recip') VALUES('$ip') WHERE username='$username'";
mysql_query($addip);

echo "You have succesfully recommended this user, <a href=\"index.php\">Back to Index</a>";

include('endcontent.php');
include('advert.php');
include('footer.php');
}
?>

$ip =$_SERVER['REMOTE_ADDR']; is included in 'header.php'

I'm pretty sure it's nowhere near working but any help will be appreciated :)

[edited by: Jeigh at 12:03 pm (utc) on June 18, 2007]

Habtom

12:07 pm on Jun 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have added and removed a few things in between, may be not a running copy, you need to check for bugs as this is not tested.

//I assume your connection is set properly.
<?php
include('header.php');
include('navigation.php');
include('content.php');

$username= $req_user_info['username'];

$reccount=$req_user_info['reccount'];

$ipcheck_query = mysql_query("SELECT recip FROM users WHERE username='$username'");
$ipcheck = mysql_fetch_array($ipcheck_query);
if($ip == $ipcheck['recip'])
{
echo "Sorry, you've already recommended this user";
echo "<a href=\"index.php\">Back to Index</a>";
}
else
{
$rec = $req_user_info['reccount'];
$addrec = $rec+1;
$recq="UPDATE users SET reccount='$addrec' WHERE username='$username'";
mysql_query($recq);

$addip="INSERT INTO users('recip') VALUES('$ip') WHERE username='$username'";
mysql_query($addip);

echo "You have succesfully recommended this user, <a href=\"index.php\">Back to Index</a>";
}
// Anyway they are called for all, so I put them here for you.
include('endcontent.php');
include('advert.php');
include('footer.php');

?>

// This seems to be impractical to me. You need a different table structure.
$addip="INSERT INTO users('recip') VALUES('$ip') WHERE username='$username'";
mysql_query($addip);

Habtom

dwighty

12:12 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



Hi Jeigh,

You are kind of along the right idea. The main thing that I would say is that you can not keep a record of the user ips in the user table.

From what i can see you have a user table which will detail the user only once and therefore everytime a new ip recommends a profile it will add their ip address and lose who ever ip was there previously. Make sense?

I would recommend that you add a new table which records the ip address's. E.g. vote_ips, then record the user_id and ip address in the table.

So when you check to see if the ip has already voted you don't check it against the user table but the vote_ips table and if this check is true then add the +1 count to the user table.

Hope this helps a bit

Paul

Jeigh

12:28 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



Thanks for the replies.

@ Habtom:

Yeah, the connection is made in 'header.php'. I'll test out the code you provided and let you know if it worked.

@ dw:

Yes, that makes sense. I want it to be one recommend per user though not just one so would I also then need to record the user they recommended in that table (so I'd have an ip column and username column?) then check the ip against the ip in the database and if it was that username?

Any suggestions on how I would attempt that (sorry if it's hard to understand, took me a while to write it :P)

Thanks a lot for the help Habtom and Dw.

Jeigh

12:48 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



I tried to code you suggested, it said it went through successfully but it didn't add to the count. I also tried adding in a bit of code from a tutorial I found which was based on a similar idea (hit counter) and also commented out anything to do with the ip just to get this part working first. Right now I'm using:

<?
include('header.php');
include('navigation.php');
include('content.php');

$username = $req_user_info['username'];

$rec = $req_user_info['reccount'];

/*$reccount = $req_user_info['reccount'];*/

/*$ipcheck =mysql_query("SELECT recip FROM users WHERE username='$username'");*/

/*if($ip == $ipcheck)
{
echo "Sorry, you've already recommended this user";
echo "<a href=\"index.php\">Back to Index</a>";

}
else
{*/

if(empty($rec)){
$reccount=1;
$recq2="INSERT INTO users(reccount) VALUES('$reccount') WHERE username='$username'";
$resultrec=mysql_query($recq2);
}

$addrec=$rec+1;
$recq="UPDATE users SET reccount='$addrec' WHERE username='$username'";
mysql_query($recq);

/*$addip="INSERT INTO users('recip') VALUES('$ip') WHERE username='$username'";
mysql_query($addip);*/

echo "You have succesfully recommended this user, <a href=\"index.php\">Back to Index</a>";
/*}*/
include('endcontent.php');
include('advert.php');
include('footer.php');

?>