1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Script security help

Discussion in 'PHP' started by hasbehas, Feb 6, 2007.

  1. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #21
    I am still waiting for an explanation as to why your super regular expressions are better than using a precompiled function. :)
    Your solution is a waste of processor cycles.
     
    Icheb, Feb 6, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #22
    your solution is non-existent, and bitchin like this is a waste of my time.

    I dont think that regular expressions will have any substantial overhead in comparison to the use of basename.
     
    krakjoe, Feb 6, 2007 IP
  3. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #23
    If there's a way to make something more efficient with minimal or maybe even less effort, you approach it. And a precompiled function that does exactly what you are trying to do is always more efficient than a regular expression.
    So if you think that your solution is better you have no clue about what scalability means, which is sad.
     
    Icheb, Feb 6, 2007 IP
  4. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #24
    Thanks to you both.. but no need to have a go at each other over this..
     
    hasbehas, Feb 7, 2007 IP
  5. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #25
    Apparently it entirely depends on your environment, no one is right or wrong, it's like I said in the first place, you have your way and I have mine .....

    
    <?php
    $mtime = explode( " ",microtime() );
    $mtime = $mtime[1] + $mtime[0];
    $starttime = $mtime; 
    
    for( $i = 0; $i < 1000; $i++ ) :
    $string = "/home/none/someone/something/contactus.htm";
    $string = preg_replace( "/\/.*\/(.*)\.(.*)$/", "$1", $string );
    endfor;
    
    $mtime = explode( " ", microtime() );
    $mtime = $mtime[1] + $mtime[0];
    $endtime = $mtime;
    $totaltime = ( $endtime - $starttime );
    
    echo "Execution time : ".$totaltime." seconds ( regex )<br />\n"; 
    
    $mtime = explode( " ",microtime() );
    $mtime = $mtime[1] + $mtime[0];
    $starttime = $mtime; 
    
    for( $i = 0; $i < 1000; $i++ ) :
    $string = "/home/none/someone/something/contactus.htm";
    $string = basename( $string );
    endfor;
    
    $mtime = explode( " ", microtime() );
    $mtime = $mtime[1] + $mtime[0];
    $endtime = $mtime;
    $totaltime = ( $endtime - $starttime );
    
    echo "Execution time : ".$totaltime." seconds ( basename )<br />\n"; 
    ?>
    
    PHP:
    10 execution basename avg : 0.508209599388 seconds
    10 execution regex avg : 0.444041623009 seconds
     
    krakjoe, Feb 7, 2007 IP
  6. aplus

    aplus Well-Known Member

    Messages:
    83
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    115
    #26
    I run the script and this is the result i got:
    i understand that basename is faster not the other way around.
     
    aplus, Feb 7, 2007 IP
  7. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #27
    On my machine locally and my server I get results the opposite of that, proving in the end that it doesn't really matter ...... it's quite bizarre infact :S

    Execution time : 0.00461602210999 seconds ( regex )
    Execution time : 0.00545215606689 seconds ( basename )

    Execution time : 0.00460600852966 seconds ( regex )
    Execution time : 0.00540685653687 seconds ( basename )

    Execution time : 0.00471901893616 seconds ( regex )
    Execution time : 0.00543093681335 seconds ( basename )
     
    krakjoe, Feb 7, 2007 IP
  8. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #28
    ROFL

    It's hilarious how you changed your post to reflect your new findings. :) Yep, that's true, I do know what you wrote there first. I especially liked that part about the lettuce.
     
    Icheb, Feb 7, 2007 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #29
    well, clearly I was partially wrong about that bit, but all the same I believe and have tested that it really does depend on your atmosphere.

    Out of interest have you run that script ? what was the results of it and what were the specs of the machine, I get pretty similar results on a dual core p4, a my athlon laptop and my p4 server, the athlon is slower but still the same sort of result ?

    Sorry about the lettuce thing it wasn't really fair ......
     
    krakjoe, Feb 7, 2007 IP
  10. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #30
    Thanks krakjoe.. it works just fine..
    The only problem that I am having is that ; I have all my functions in a folder a file named functions.php
    I moved the function to functions.php
    So I include the php/functions.php in details.php
    But it doest not work then.. "Unrecognized title, Unrecognized content " Any idea why ?
     
    hasbehas, Feb 7, 2007 IP
  11. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #31
    
    function get_details( $pid )
    
    {
    
      # Clean up the pid
    
      $pid = preg_replace( "/\.(.*)$/", "", $pid );
    
      # I'm being anal, no need for this ( for most machines )
    
      $return = array(); 
    
      # Might want this to log foul requests
    
      $return["requester"] = $_SERVER['REMOTE_ADDR'];
    
      # While we're here :
    
      $return["time"] = time();
    
      # User agent into array ...
    
      $return["ua"] = $_SERVER['HTTP_USER_AGENT'];  
    
      # Need to check that we're making sense still
    
      if( !file_exists( ROOT_PATH . "/" . $pid . ".htm" ) )
    
      {
    
        $return["content"] = "Unrecognized content";
    
      }
    
      else
    
      {
    
        $return["content"] = @file_get_contents( ROOT_PATH . "/" . $pid . ".htm" );
    
      }
    
    
    
      if( !file_exists( ROOT_PATH . "/" . $pid . ".txt" ) )
    
      {
    
        $return["title"] = "Unrecognized title";
    
      }
    
      else
    
      {
    
        $return["title"] = @file_get_contents( ROOT_PATH . "/" . $pid . ".txt" );
    
      }
    
    
    
      # If you had meta desc for pages, you could do the following also :
    
      # $return["meta"] = @file_get_contents( dirname(__FILE__) . "/" . $pid . ".meta" ) ; 
    
      # but you don't so I'll leave it commented, but you see how it's working now
    
      # right?  
    
      return $return; # Return data, an associative array of all sorts
    
    }
    
    PHP:
    put that in functions, and in the file you're calling it from, before you include the function file put :

    
    define("ROOT_PATH", dirname(__FILE__) );
    
    PHP:
    and that'll sort that out for you assuming your details.php is in same folder as stuff.htm and stuff.txt
     
    krakjoe, Feb 7, 2007 IP