Will This Code Work . . . ?

Discussion in 'PHP' started by Masterful, Oct 4, 2008.

  1. #1
    Will the following code work? I can't test it for various reasons. It must, must work first time round, so please be thorough with your evaluations.

    Basically, I want to count how many clicks my links get. I've put my links into a database. When someone clicks a link on my site (which look like this: goto.php?id=1), they are sent to goto.php, which:

    • gets the ID
    • connects to the database
    • updates the Count column of the link table
    • selects the real link (note: this has a variable appended to it: http://www.whatever.com?=$var)
    • redirects the visitor to the location

    <?php
    
    $id = intval($_GET['id']);
    
    $conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR); 
    mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);
    
    mysql_query("UPDATE table SET count = count+1 WHERE id=$id"); 
    
    $href = "SELECT url FROM table WHERE id=$id";
    
    $qry = mysql_query($href);
    
    list($href)=mysql_fetch_row($qry); // list() because an array is returned
    
    eval("\$href = \"$href\";");
    
    header("Location:$href");
    
    mysql_close($conn);
    
    ?> 
    PHP:
    What I am particularly worried about are the variables appended to the links in the database. Will they be echoed properly?
     
    Masterful, Oct 4, 2008 IP
  2. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Just to be clear . . . I'm not asking anyone to test this for me. All I want is for anyone to tell me if they can see any obvious errors.
     
    Masterful, Oct 4, 2008 IP
  3. fireworking

    fireworking Peon

    Messages:
    460
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I dont understand what you are trying to say. Just install xampp on your computer and test it.
     
    fireworking, Oct 4, 2008 IP
  4. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #4
    You see, the only way for me to test this code properly is to click a link on my site, go to a store, buy something, and see if the variable in the URLs appears in my affiliate network account.

    All I'm asking, therefore, is if anyone can see any obvious errors. If not, that's great. :)
     
    Masterful, Oct 4, 2008 IP
  5. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #5
    Not to worry . . I've managed to test it. It works!
     
    Masterful, Oct 4, 2008 IP
  6. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #6
    Always use mysql_fetch_assoc instead of mysql_fetch_row. This will avoid further complications and editing if you change the query.

    Peace,
     
    Barti1987, Oct 4, 2008 IP
  7. bm_ys

    bm_ys Well-Known Member

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    #7
    yes this code will work, but you must write the same code for other variables used on your site.
     
    bm_ys, Oct 5, 2008 IP
  8. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #8
    Please elaborate. What do you mean?
     
    Masterful, Oct 5, 2008 IP
  9. bm_ys

    bm_ys Well-Known Member

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    131
    #9
    if you use variables other then id, like p,page,s,q,cat... you need to modify your code for these variables.

    goto.php?id=1
    goto.php?id=1&cat=5
    goto.php?id=1&cat=6..

    in these links there is cat variable other then id variable so your code will look like

    $id = intval($_GET['id']);
    $cat = intval($_GET['cat']);

    or another way without modification is

    you'll create a table with entry_id, url, count fields and make entry_id autoincrement and in the begining of your header file you will insert this code
    <?php
    //$id = intval($_GET['id']);
    $url = $_SERVER['QUERY_STRING'];
    $conn = mysql_connect('localhost','root') or trigger_error("SQL", E_USER_ERROR); 
    mysql_select_db('ctyi', $conn) or trigger_error("SQL", E_USER_ERROR);
    
    #control if url inserted before
    $control = mysql_result(mysql_query("SELECT entry_id FROM table where url='$url'"),0);
    if(empty($control))
    	{
    	#if url not inserted before make insert
    	mysql_query("INSERT into table VALUES(NULL,'$url','1')");//1 for first count
    	}
    	else
    	{
    	#if url inserted before make update
    	mysql_query("UPDATE table SET count = count+1 WHERE entry_id='$control'"); 
    	}
    $url = $_SERVER['PHP_SELF'].'?'.$url;
    header("Location:$url");
    mysql_close($conn);
    ?>
    Code (markup):
    but if you only use id as http variable you dont need any modification in your script your script will work.
     
    bm_ys, Oct 5, 2008 IP