SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP Counter to an XML file

    I have a PHP counter on my website and it writes the count to a text file as it incraments, but it also writes the visiting person's IP to another text file. I want to be able to write the IP's to an XML file, anyone know how I could edit my script to do that? Here's a copy of my current script (counter.php)

    PHP Code:
    <?php 
    session_start
    ();     
    session_register('counter');        
    $CountFile "txtfolder/counter.txt";     
    $Count file($CountFile);     
    $Count implode(""$Count);     
    if(!
    $_SESSION['counter']){     
    $_SESSION['counter'] = true
    $OpenFile fopen($CountFile"r+");     
    $Count++;     
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);     
    $banned = array('24.000.00.00''142.00.00.00');     
    if (!
    in_array($user_ip$banned)) {     
      if(
    $OpenFile){     
      
    fwrite($OpenFile$Count);     
      
    fclose($OpenFile);     
      }       
    }     
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 

    $string file_get_contents('txtfolder/ipaddresses.txt'); 
    $pos strpos($string $user_ip); 
    $ip_file "txtfolder/ipaddresses.txt"
    $ip fopen($ip_file"a+"); 
    if (
    $pos === false) { 
        
    fwrite($ip"$user_ip\n");     
        
    fclose($ip); 

    }    
    ?>
    This is kinda what I wanted it to look like:

    Code:
    <?xml version="1.1"?>
    
    <iplist>
        <ipaddresses>
            <ip address="000.000.000.000">
            <ip address="999.999.999.999">
        </ipaddresses>
    </iplist>
    I was hoping to add other values to the ip field later like:

    browser="IE"
    referer="google.com"

    but those are another issue for a later date, unless anyone can suggest a way to do that aswell

    Thanks!

  2. #2
    SitePoint Zealot s21825's Avatar
    Join Date
    Oct 2003
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, XML tags can't contain spaces. See the w3schools article.

    You could do this ... it's a bit hacky though:
    PHP Code:
    if ($pos === false) {
        
    fwrite($ip"$user_ip\n");     
        
    fclose($ip);

    could be changed to:
    PHP Code:
    if ($pos === false) {
        
    fwrite($ip"<ipAddress>$user_ip</ipAddress>\n");     
        
    fclose($ip);

    Then when you want to make it well-formed XML you could just stick
    Code:
    <?xml version="1.1"?>
    
    <iplist>
        <ipaddresses>
    at the beginning and stick
    Code:
        </ipaddresses>
    </iplist>
    at the end. I'm sure there is a way to find the </ipaddresses> tag and add you new entry into the file at that point, but I'm not that familiar with working with files ... so I don't know off the top of my head.
    s21825

  3. #3
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use the dom. I'll go out on a limb and assume you're using php4, in which case you should use www.php.net/dom. If you're php5 you should use www.php.net/domxml, but the code below is php4 (I assume you have $ip_file_name and $user_ip, as well as $browser and $referer for future needs):

    PHP Code:
    ;
    // first, load the file
    $document = &domxml_open_file($ip_file_name);

    // now, let's get the ipaddresses tag
    $tags = &$document->get_elements_by_tagname('ipaddresses');

    // let's really be sure we got what we wanted
    if(empty($tags))
        {
        
    // do whatever would happen on fail
        
    }
    else
        {
        
    $ip_addresses = &$tags[0];
        
    $ip = &$document->create_element('ip');
        
    $ip->set_attribute('address'$user_ip);
        
    $ip->set_attribute('browser'$browser);
        
    $ip->set_attribute('refere'$referer);
        
    $ip_addresses->append_child($ip);
        
    $document->dump_file($ip_file_namefalsetrue);
        } 
    Hope that helps! Feel free to ask any questions (I know I was QUITE intimidated by the domxml extension when I started playing with it). Also, a couple of notes about your xml you may need to consider:
    (1)use xml version 1.0. Version 1.1 isn't widely supported.
    (2)Make sure your document is well formed. Domxml will break if it opens a file like the one you posted (I copy and pasted it to test with... <ip address="999.999.999.999"> SHOULD BE <ip address="999.999.999.999"/>)
    (3) You may consider using tags for each attribute, but that is personal preference and doesn't matter. I would do something like this (but it doesn't mean it's the "right" way):
    Code:
    <?xml version="1.0"?>
    
    <visitorList>
     <visitor>
      <ipAddress>000.000.000.000</ipAddress>
      <browser>Firefox</browser>
      <referer>Google</referer>
     </visitor>
    <visitorList>
    Of course, someone may look at that above and scream "BLOAT" ... I just find it easier on the eyes. The above would be no different really as

    Code:
    <?xml version="1.0"?>
    
    <visitorList>
     <visitor ipAddress="000.000.000.000" browser="Firefox" referer="Google"/>
    <visitorList>
    It's just that I've seen xml tags with 30 attributes, and that scared me.

    Edit:


    Oh, and naturally, make sure the xml file is writable by the web server.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys!

    OfiiceOfTheLaw: That worked perfectly, except one thing I forgot, how do I get it to look and see if the IP has already been stored in the file and if it has to skip it and not store it again? Other than that it was perfect thanks again!

  5. #5
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do an xpath query to find what you want. There may be a better way to do it (I won't pretend to be an xpath expert) but you could do something like execute /ipList/ipaddresses/ip and foreach over the returned nodeset, checking to see if /ipList/ipaddresses/ip/@address is equal to the address. (I am sure there is a better way though).

    See http://us3.php.net/manual/en/function.xpath-eval.php
    and
    http://www.zvon.org/xxl/XPathTutoria.../examples.html

    Edit:


    Yeah, finally remembered a more ideal way. Instead of looping over the nodeset, you could simply run one xpath expression that will retrieve the node if it exists. Here's how I did it before in an xml document that had questions for a quiz in it and I needed to select a question by it's title:

    /quiz/questions/question[@title="foo"]

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  6. #6
    SitePoint Enthusiast aLargeTrout's Avatar
    Join Date
    Sep 2005
    Location
    Bucharest, Romania
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try an XPath expression:

    PHP Code:
    $document domxml_open_file($ip_file_name);
    $xp $document->xpath_new_context();
    print_r($xp->xpath_eval("//ipAddress = '000.000.000.000'"));
    print_r($xp->xpath_eval("//ipAddress = '34534.45645.456.6'")); 

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I'm having trouble understanding the xpath expression. Could you maybe give me an example?? Also I've got the $user_ip and $browser working but I can't seem to get my sintax right to get the referer to be displayed, any ideas?
    Here's what my code looks like now:

    PHP Code:
    <?php 
    session_start
    ();   
    session_register('counter');      
    $CountFile "txtfolder/counter.txt";   
    $Count file($CountFile);   
    $Count implode(""$Count);   
    if(!
    $_SESSION['counter']){   
    $_SESSION['counter'] = true;
    $OpenFile fopen($CountFile"r+");   
    $Count++;   
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);
    $browser get_browser($_SERVER['HTTP_USER_AGENT']);
    $referer getenv($_SERVER['HTTP_REFERER']);  
    $banned = array('000.000.000.000''999.999.999.999');   
    if (!
    in_array($user_ip$banned)) {   
      if(
    $OpenFile){   
      
    fwrite($OpenFile$Count);   
      
    fclose($OpenFile);   
      }     
    }   
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 
    }
    //
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
       {
         
    $browser 'Netscape';
       }
       else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       {
         
    $browser 'Firefox';
       }
       else
       {
         
    $browser 'Mozilla';
       }
    }
    else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
       {
         
    $browser 'Opera';
       }
       else
       {
         
    $browser 'Internet Explorer';
       }
    }
    else
    {
       
    $browser 'Others Browser';
    }
    //
    $string file_get_contents('txtfolder/ipaddresses.xml'); 
    $pos strpos($string $user_ip);
    $ip_file "txtfolder/ipaddresses.xml";
    $document = &domxml_open_file($ip_file);
    $tags = &$document->get_elements_by_tagname('ipaddresses');
    if(empty(
    $tags)) 
        {
        } 
    else 
        { 
        
    $ip_addresses = &$tags[0]; 
        
    $ip = &$document->create_element('ip'); 
        
    $ip->set_attribute('address'$user_ip);
        
    $ip->set_attribute('browser'$browser); 
        
    $ip->set_attribute('referer'$referer);
        
    $ip_addresses->append_child($ip); 
        
    $document->dump_file($ip_filefalsetrue); 
        }
    }  
    ?>

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I made some changes but the referer is still not being displayed lol and of course still no luck with the xpath expression ...

    PHP Code:
    <?php 
    session_start
    ();   
    session_register('counter');      
    $CountFile "txtfolder/counter.txt";   
    $Count file($CountFile);   
    $Count implode(""$Count);   
    if(!
    $_SESSION['counter']){   
    $_SESSION['counter'] = true;
    $OpenFile fopen($CountFile"r+");   
    $Count++;   
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);
    $browser get_browser($_SERVER['HTTP_USER_AGENT']);
    $domain 'php.net';  
    $banned = array('000.000.000.000''999.999.999.999');  
    if (!
    in_array($user_ip$banned)) {   
      if(
    $OpenFile){   
      
    fwrite($OpenFile$Count);   
      
    fclose($OpenFile);   
      }     
    }   
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 
    }
    //
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
       {
         
    $browser 'Netscape';
       }
       else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       {
         
    $browser 'Firefox';
       }
       else
       {
         
    $browser 'Mozilla';
       }
    }
    else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
       {
         
    $browser 'Opera';
       }
       else
       {
         
    $browser 'Internet Explorer';
       }
    }
    else
    {
       
    $browser 'Other Browsers';
    }
    //
    $string file_get_contents('txtfolder/ipaddresses.xml'); 
    $pos strpos($string $user_ip);
    $ip_file "txtfolder/ipaddresses.xml";
    $document = &domxml_open_file($ip_file);
    $tags = &$document->get_elements_by_tagname('ipaddresses');
    if(empty(
    $tags)) 
        {
        } 
    else 
        { 
        
    $ip_addresses = &$tags[0]; 
        
    $ip = &$document->create_element('ip'); 
        
    $ip->set_attribute('address'$user_ip);
        
    $ip->set_attribute('browser'$browser); 
        function 
    refer() {

            global 
    $domain;
            
    $referer getenv($_SERVER['HTTP_REFERER']); // Where did the person come from?

            
    if (($referer) and (!strstr($referer$domain))) {    // if there's a referrer, and it's not someone on your site

                
    $ip->set_attribute('referer'$referer);

            }

        }
        
    $ip_addresses->append_child($ip); 
        
    $document->dump_file($ip_filefalsetrue); 
        }
    }  
    ?>

  9. #9
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I don't want to spoon feed you the exact answer as I feel the best way to learn is on your own, but I will give you an identical example of one way it could be done with a different xml file. If you plan on working with the Dom, you should really learn xpath as it's great to be able to run xpath expressions to fetch a node or nodeset, update the data contained within it, and the rewrite the data in the document with it.

    Anyway, here's an example xml file and some example php code to get what I want from it:

    Code:
    <?xml version="1.0"?>
    
    <quiz>
     <questions>
      <question text="What is your name?" ident="I0001"/>
      <question text="What is your age?" ident="I0002"/>
      <question text="Are you a student?" ident="I0003"/>
     </questions>
    </quiz>
    PHP Code:
    <?php

    // open the file (or you could already have a dom document open
    $doc = &domxml_open_file('./quiz.xml');

    // create xpath context
    $ctx = &xpath_new_context($doc);
    $node = &$ctx->xpath_eval('/quiz/questions/question[@ident="I0002"]');

    if(empty(
    $node->nodeset))
        {
        
    // node doesn't exist
        
    }
    else
        {
        echo 
    $node->nodeset[0]->get_attribute('text');
        }

    ?>
    Hope that helps.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  10. #10
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brad_Armitage
    Ok I made some changes but the referer is still not being displayed lol and of course still no luck with the xpath expression ...

    PHP Code:
    <?php 
    session_start
    ();   
    session_register('counter');      
    $CountFile "txtfolder/counter.txt";   
    $Count file($CountFile);   
    $Count implode(""$Count);   
    if(!
    $_SESSION['counter']){   
    $_SESSION['counter'] = true;
    $OpenFile fopen($CountFile"r+");   
    $Count++;   
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);
    $browser get_browser($_SERVER['HTTP_USER_AGENT']);
    $domain 'php.net';  
    $banned = array('000.000.000.000''999.999.999.999');  
    if (!
    in_array($user_ip$banned)) {   
      if(
    $OpenFile){   
      
    fwrite($OpenFile$Count);   
      
    fclose($OpenFile);   
      }     
    }   
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 
    }
    //
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
       {
         
    $browser 'Netscape';
       }
       else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       {
         
    $browser 'Firefox';
       }
       else
       {
         
    $browser 'Mozilla';
       }
    }
    else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
       {
         
    $browser 'Opera';
       }
       else
       {
         
    $browser 'Internet Explorer';
       }
    }
    else
    {
       
    $browser 'Other Browsers';
    }
    //
    $string file_get_contents('txtfolder/ipaddresses.xml'); 
    $pos strpos($string $user_ip);
    $ip_file "txtfolder/ipaddresses.xml";
    $document = &domxml_open_file($ip_file);
    $tags = &$document->get_elements_by_tagname('ipaddresses');
    if(empty(
    $tags)) 
        {
        } 
    else 
        { 
        
    $ip_addresses = &$tags[0]; 
        
    $ip = &$document->create_element('ip'); 
        
    $ip->set_attribute('address'$user_ip);
        
    $ip->set_attribute('browser'$browser); 
        function 
    refer() {

            global 
    $domain;
            
    $referer getenv($_SERVER['HTTP_REFERER']); // Where did the person come from?

            
    if (($referer) and (!strstr($referer$domain))) {    // if there's a referrer, and it's not someone on your site

                
    $ip->set_attribute('referer'$referer);

            }

        }
        
    $ip_addresses->append_child($ip); 
        
    $document->dump_file($ip_filefalsetrue); 
        }
    }  
    ?>

    That function smack dab in middle of the else statement is rather nasty, no? And the global is pretty rough too. Let's change it to this and put it at the top of your script:

    PHP Code:
    function refer(&$ip$domain$referer
            {
            if ((
    $referer) and (!strstr($referer$domain))) 
                {
                
    // if there's a referrer, and it's not someone on your site
                
    $ip->set_attribute('referer'$referer);
                }

            }

    // later we call it, presumably where you had the function definition smack dab in middle of procedural code

     
    $ip_addresses = &$tags[0];
        
    $ip = &$document->create_element('ip');
        
    $ip->set_attribute('address'$user_ip);
        
    $ip->set_attribute('browser'$browser);

        
    referer($ip$domaingetenv($_SERVER['HTTP_REFERER']) );

        
    $ip_addresses->append_child($ip);
        
    $document->dump_file($ip_filefalsetrue); 
    Now, as you noticed, the first paramerte I have for referer is &$ip, which passes the DomElement object by reference. This is why your last function didn't work anyway... it wasn't getting the ip from anywhere. I hae also removed the nasty global as well.

    Hope that helps, feel free to ask any questions.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what I did wrong there thanks! However the referer field is not even showing up now let alone displaying the referer?

    PHP Code:
    <?php 
    session_start
    ();   
    session_register('counter');      
    $domain 'bradleya.ca';
    function 
    refer(&$ip$domain$referer

    if ((
    $referer) and (!strstr($referer$domain))) 
        { 
        
    // if there's a referrer, and it's not someone on your site 
        
    $ip->set_attribute('referer'$referer); 
        } 

    }
    $CountFile "txtfolder/counter.txt";   
    $Count file($CountFile);   
    $Count implode(""$Count);   
    if(!
    $_SESSION['counter']){   
    $_SESSION['counter'] = true;
    $OpenFile fopen($CountFile"r+");   
    $Count++;   
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);
    $browser get_browser($_SERVER['HTTP_USER_AGENT']);
    $banned = array(); // '24.215.68.34', '142.67.28.35'  
    if (!in_array($user_ip$banned)) {   
      if(
    $OpenFile){   
      
    fwrite($OpenFile$Count);   
      
    fclose($OpenFile);   
      }     
    }   
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 
    }
    //
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
       {
         
    $browser 'Netscape';
       }
       else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       {
         
    $browser 'Firefox';
       }
       else
       {
         
    $browser 'Mozilla';
       }
    }
    else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
       {
         
    $browser 'Opera';
       }
       else
       {
         
    $browser 'Internet Explorer';
       }
    }
    else
    {
       
    $browser 'Other Browsers';
    }
    //
    $string file_get_contents('txtfolder/ipaddresses.xml'); 
    $pos strpos($string $user_ip);
    $ip_file "txtfolder/ipaddresses.xml";
    $document = &domxml_open_file($ip_file);
    $tags = &$document->get_elements_by_tagname('ipaddresses');
    if(empty(
    $tags)) 
        {
        } 
    else 
        {
        
    $ip_addresses = &$tags[0]; 
        
    $ip = &$document->create_element('ip'); 
        
    $ip->set_attribute('address'$user_ip); 
        
    $ip->set_attribute('browser'$browser); 

        
    refer($ip$domaingetenv($_SERVER['HTTP_REFERER']) ); 

        
    $ip_addresses->append_child($ip); 
        
    $document->dump_file($ip_filefalsetrue);
        }
    }  
    ?>
    Here's what the xml file looks like now:

    HTML Code:
    <?xml version="1.0" encoding="iso-8859-1" ?> 
    <iplist>
      <ipaddresses>
        <ip address="65.54.188.138" browser="Other Browsers" referer="" /> 
        <ip address="000.000.000.000" browser="Internet Explorer" /> 
        <ip address="000.000.000.000" browser="Internet Explorer" /> 
      </ipaddresses>
    </iplist>

  12. #12
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var_dump(getenv($_SERVER['HTTP_REFERER']));

    and

    var_dump($_SERVER['HTTP_REFERER']);

    to make sure you are passing the correct referer string I suppose.... make sure something is getting passed in.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  13. #13
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    string(60) "http://www.metacrawler.com/info.metac/search/web/bradleya.ca" 
    I tried getting to my site from a search engine.
    So it's just not being displayed in the referer field but being dumped into the index.php file?

  14. #14
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm.. should work then... did you try echoing $referer when you pass it to the function?

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  15. #15
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, and still nothing

    PHP Code:
    <?php 
    session_start
    ();   
    session_register('counter');      
    $domain 'bradleya.ca';
    function 
    refer(&$ip$domain$referer

    if ((
    $referer) and (!strstr($referer$domain))) 
        { 
        
    // if there's a referrer, and it's not someone on your site 
        
    $ip->set_attribute('referer'$referer);
        } 

    }
    $CountFile "txtfolder/counter.txt";   
    $Count file($CountFile);   
    $Count implode(""$Count);   
    if(!
    $_SESSION['counter']){   
    $_SESSION['counter'] = true;
    $OpenFile fopen($CountFile"r+");   
    $Count++;   
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);
    $browser get_browser($_SERVER['HTTP_USER_AGENT']);
    $banned = array(); 
    if (!
    in_array($user_ip$banned)) {   
      if(
    $OpenFile){   
      
    fwrite($OpenFile$Count);   
      
    fclose($OpenFile);   
      }     
    }   
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 
    }
    //
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
       {
         
    $browser 'Netscape';
       }
       else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       {
         
    $browser 'Firefox';
       }
       else
       {
         
    $browser 'Mozilla';
       }
    }
    else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
       {
         
    $browser 'Opera';
       }
       else
       {
         
    $browser 'Internet Explorer';
       }
    }
    else
    {
       
    $browser 'Other Browsers';
    }
    //
    $string file_get_contents('txtfolder/ipaddresses.xml'); 
    $pos strpos($string $user_ip);
    $ip_file "txtfolder/ipaddresses.xml";
    $document = &domxml_open_file($ip_file);
    $tags = &$document->get_elements_by_tagname('ipaddresses');
    if(empty(
    $tags)) 
        {
        } 
    else 
        {
        
    $ip_addresses = &$tags[0]; 
        
    $ip = &$document->create_element('ip'); 
        
    $ip->set_attribute('address'$user_ip); 
        
    $ip->set_attribute('browser'$browser);
        
    refer($ip$domaingetenv($_SERVER['HTTP_REFERER']));
        
    var_dump($_SERVER['HTTP_REFERER']);
        
    $ip_addresses->append_child($ip); 
        
    $document->dump_file($ip_filefalsetrue);
        }
    }  
    ?>

  16. #16
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I mean from within the function.. somewhere you seem to lose the referer... that function should put whatever string you passed for $referer into the referer attribute.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  17. #17
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean like this?

    PHP Code:
    function refer(&$ip$domain$referer

    if ((
    $referer) and (!strstr($referer$domain))) 
        { 
        
    // if there's a referrer, and it's not someone on your site 
        
    $ip->set_attribute('referer'$referer);
        
    var_dump($_SERVER['HTTP_REFERER']);
        } 



  18. #18
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this
    PHP Code:
    function refer(&$ip$domain$referer

    var_dump($referer);
    if ((
    $referer) and (!strstr($referer$domain))) 
        { 
        
    // if there's a referrer, and it's not someone on your site 
        
    $ip->set_attribute('referer'$referer);
        } 


    That way you can know for sure if anything was being passed in

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  19. #19
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK now I'm getting this error msg:

    bool(false)

  20. #20
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Such as I suspected... make sure you are passing the referer to the function. You may want to pass $_SERVER['HTTP_REFERER'] rather than getenv($_SERVER['HTTP_REFERER']) to it.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  21. #21
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok now it's giving me a similar output on my index.php as before, but still not writing to the referer field:

    HTML Code:
    string(180) "http://www.metacrawler.com/info.metac.toolbar/search/web/bradleya.ca/1/-/1/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/417/top" 
    PHP Code:
    <?php 
    session_start
    ();   
    session_register('counter');      
    $domain 'bradleya.ca';
    function 
    refer(&$ip$domain$referer

    var_dump($referer); 
    if ((
    $referer) and (!strstr($referer$domain))) 
        { 
        
    // if there's a referrer, and it's not someone on your site 
        
    $ip->set_attribute('referer'$referer); 
        } 

    }
    $CountFile "txtfolder/counter.txt";   
    $Count file($CountFile);   
    $Count implode(""$Count);   
    if(!
    $_SESSION['counter']){   
    $_SESSION['counter'] = true;
    $OpenFile fopen($CountFile"r+");   
    $Count++;   
    $user_ip GetHostByName($_SERVER['REMOTE_ADDR']);
    $browser get_browser($_SERVER['HTTP_USER_AGENT']);
    $banned = array();  
    if (!
    in_array($user_ip$banned)) {   
      if(
    $OpenFile){   
      
    fwrite($OpenFile$Count);   
      
    fclose($OpenFile);   
      }     
    }   
    if (isset (
    $_SERVER['HTTP_X_FORWARDED_FOR'])) { 
       
    $user_ip $_SERVER['HTTP_X_FORWARDED_FOR']; 
    } else { 
       
    $user_ip $_SERVER['REMOTE_ADDR']; 
    }
    //
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
       {
         
    $browser 'Netscape';
       }
       else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
       {
         
    $browser 'Firefox';
       }
       else
       {
         
    $browser 'Mozilla';
       }
    }
    else if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
    {
       if ( 
    strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
       {
         
    $browser 'Opera';
       }
       else
       {
         
    $browser 'Internet Explorer';
       }
    }
    else
    {
       
    $browser 'Other Browsers';
    }
    //
    $string file_get_contents('txtfolder/ipaddresses.xml'); 
    $pos strpos($string $user_ip);
    $ip_file "txtfolder/ipaddresses.xml";
    $document = &domxml_open_file($ip_file);
    $tags = &$document->get_elements_by_tagname('ipaddresses');
    if(empty(
    $tags)) 
        {
        } 
    else 
        {
        
    $ip_addresses = &$tags[0]; 
        
    $ip = &$document->create_element('ip'); 
        
    $ip->set_attribute('address'$user_ip); 
        
    $ip->set_attribute('browser'$browser);
        
    refer($ip$domain$_SERVER['HTTP_REFERER']);
        
    $ip_addresses->append_child($ip); 
        
    $document->dump_file($ip_filefalsetrue);
        }
    }  
    ?>

  22. #22
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have not really looked through it, but I would assume you may need to ensure your if block is evaluating to true.

    You should note that (!strstr($referer, $domain)) is returning false in your example if I understand the contents of those two variables correctly. You are checking to see if 'bradleya.ca' is in "http://www.metacrawler.com/info.metac.toolbar/search/web/bradleya.ca/1/-/1/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/417/top" and it is. Your if block should never run it seems.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  23. #23
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it must be evaluating to false because the referer field isn't even showing up. I'm checking to see if bradleya.ca is in the referel url? Why is it doing that?? I'm not sure I understand here I went wrong

  24. #24
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    NS, Canada
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well thanks for the help Anyone else care to take a shot? lol


Bookmarks

Posting Permissions

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