Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post

    & symbol in address line is screwing up the script

    Can anyone tell me how to fix this. I have this link click counting script that works great except if a link has an "&" symbol in it. If it does then it disregards the & and the rest of the url and writes only the part up until that point into the database. It also redirects to the shortened url afterwards.

    What can I add to get around that?

    PHP Code:
    <?php 
    require 'connect.php';

    if(!empty(
    $HTTP_GET_VARS['j'])) 


        
    $b=mysql_select_db("news"$con);
        
    $date time();
        
        
    $j=addslashes($HTTP_GET_VARS['j']); 
        
    $c=mysql_query("select id from click_counter where url='$j' order by id desc limit 1"); 
        
    $d=mysql_fetch_object($c); 
        if(
    is_object($d)) 
        { 
            
    mysql_query("update click_counter set hits=hits+1 where id={$d->id}"); 
        } 
        else
        {
            
    mysql_query("insert into click_counter (url, date)values('$j',' $date')");
        }
        
    header("Location: $j");
    }
    else
    {
        
    header("Location: http://{$HTTP_SERVER_VARS['SERVER_NAME']}");
    }
    ?>

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    try urlencode()

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rafiki View Post
    try urlencode()
    No luck. It still stops at that &.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    If you post an example of the link/data that does not work and also show what the results should be, it would help someone see what you are trying to accomplish.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by CFMaBiSmAd View Post
    If you post an example of the link/data that does not work and also show what the results should be, it would help someone see what you are trying to accomplish.
    This is the link that I use. It works fine on any links I've used except for this one with the & symbol in it.

    Code:
    http://woopig.net/news/cc.php?j=http://www.hogwired.com/ViewArticle.dbml?DB_OEM_ID=6100&ATCLID=1064637
    This is what gets written to my database as well as where I am directed to:

    http://www.hogwired.com/ViewArticle.dbml?DB_OEM_ID=6100

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Do a string replace, replacing instances of & with &amp;
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Do a string replace, replacing instances of & with &amp;
    Can that be automated or will I have to do it every time someone posts a link?

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    The encoded &amp; will be interpreted as & when received by the server and it will terminate the $_GET['j'] variable (I just tested it.) To use a $_GET variable, you would need to change the & into something else in the link and then change it back within your code (messy.)

    You might want to look at the $_SERVER['QUERY_STRING'] variable and parse that to get what you want. For your example, this would give (tested) -
    Code:
    j=http://www.hogwired.com/ViewArticle.dbml?DB_OEM_ID=6100&ATCLID=1064637
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #9
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by CFMaBiSmAd View Post
    The encoded &amp; will be interpreted as & when received by the server and it will terminate the $_GET['j'] variable (I just tested it.) To use a $_GET variable, you would need to change the & into something else in the link and then change it back within your code (messy.)

    You might want to look at the $_SERVER['QUERY_STRING'] variable and parse that to get what you want. For your example, this would give (tested) -
    Code:
    j=http://www.hogwired.com/ViewArticle.dbml?DB_OEM_ID=6100&ATCLID=1064637
    I can't seem to make much since out of the $_SERVER['QUERY_STRING'] idea. I'm a novice and I got this code on a google search. I don't understand where to use it and/what to replace with it.

  • #10
    Regular Coder
    Join Date
    Jan 2007
    Posts
    142
    Thanks
    9
    Thanked 1 Time in 1 Post
    Diligence and determination finally paid off. I got it working with your suggestion and parsing it with substr.
    PHP Code:
    $m=addslashes($_SERVER['QUERY_STRING']);
    $j=substr($m,2); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •