Cookie and counter problem

Discussion in 'PHP' started by simnorwebdesign, Mar 15, 2008.

  1. #1
    I have a counter for each profile on a site, however it doesn't seem to be working. I want it to set a cookie and if a cookie is present it wont increment the value in profileViews in the database, the code I have is:

    
    $username1 = $row['username'];
    $cookiename = "stpcounter".$username1;
    if (!isset($_COOKIE['$cookiename'])) {
    	$count= $row['profileViews'] + 1;
    	mysql_query("UPDATE profile SET profileViews='$count' WHERE username = '$username1'");
    	setcookie('$cookiename', 'set', time()+86400);
    }
    Code (markup):
    Does anybody know why this isn't working

    Thanks

    Simon
     
    simnorwebdesign, Mar 15, 2008 IP
  2. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Firstly that query is just asking for an sql injection.

    You really need to go and at a min es shell that.


    Also should do some ereg or str replace on some operands.

    Anyhow, on to your problem.

    I would check cookie name and make sure it exists and is correct.

    I would also do a print_r($_COOKIE);

    And check the value.

    Chances are you are calling a cookie name that does not exists.

    You are not showing us how your getting your username.

    If they PHP_AUTH you should use the environmental variable rather then a database call.

    But I cannot see how you are populating username.

    $row['username']; means nothing if the query to create the $row array is inccorrect.
     
    LittleJonSupportSite, Mar 15, 2008 IP