Code Share - Page 2
Page 2 of 8 FirstFirst 1234 ... LastLast
Results 16 to 30 of 109

Thread: Code Share

  1. #16
    Better fan than rebelo! Moonglobe's Avatar
    Join Date
    Apr 2003
    Location
    brain://localhost:left-side
    Posts
    2,044
    here ya go...... goog god this is old, from like may. i'd almost forgotten about it.......
    there's no place i can be, since i found serenity.

  2. #17
    Unrated... The Chancer's Avatar
    Join Date
    Aug 2002
    Location
    Flying... always flying (apart from when crashed)
    Posts
    1,253
    Urm...

    <?php
    echo phpinfo();
    ?>

    Does that count ??

    But in addition to that... the attached displays an e-mail message of (almost) any variety, and also includes inline images...
    The Chancer
    ----------------
    Does This Mean I'm Bored

  3. #18
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,572
    Okay, this one is only useful if you don't want to do any error checking, but you can make it a bit more robust with a bit of work. It's mainly a silly little bauble to show off preg_replace()'s /e modifier.

    It's a recursive template-evaluation function: file inclusions are specified by the syntax [%filename%], and the included files may of course themselves include files in the same way (Like I said: No Error Checking. I've never actually used it). You kick it off with some text that already contains some inclusions. It might be amusing to work out which order the substitutions are made in. Then again, it might not. Depends on what turns you on, I guess.

    Code:
    function file_inclusions($text)
    {	return preg_replace
    		("/\[%(.+?)%\]/e",
    		"file_inclusions(file_get_contents('$1'))",
    		$text);
    }
    [Edit: Good grief, I just tried it and it actually works!]

    [Edit: Ten Years Later... the /e modifier is deprecated as of PHP 5.5 in favour of preg_replace_callback - like eval(), it's way too powerful and dangerous and evil to be allowed out (it's all too easy to inject arbitrary code that will be evaluated).]

    [Edit: Later still... the /e modifier is gone.
    PHP Code:
    function file_inclusions($text)
    {
        return 
    preg_replace_callback("/\[%(.+?)%\]/", function($m)
        {
            return 
    file_inclusions(file_get_contents($m[1]));
        }, 
    $text);

    ]
    Last edited by Weedpacket; 12-08-2017 at 08:13 PM. Reason: Outdated code.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #19
    Country Bumpkin OhLordy's Avatar
    Join Date
    Feb 2003
    Location
    Surrey / UK
    Posts
    509
    Here's a little script that returns the highest common denominator (think that's what it's called) handy for making nice to display ratios eg "The uploaded image mus have a ratio of 4:6"

    PHP Code:
    <?
    function get_divisor($x, $y) 

      if($x%1!=0 || $y%1!=0) 
        return false; 
      for($i=($x>$y?$y:$x);$i>0;$i--) 
      {
        if($x%$i==0 && $y%$i==0)
          return $i;
      }
    }
    ?>
    Also here's a little class from the Wrox Pro PHP4 book which I find very handy for profiling scripts

    PHP Code:
    <?
    class timer
    {
        var $timers = array();

        function timer()
        {
            // Nothing
        }

        function timerStart($name = 'default')
        {
            $time_portions = explode(' ',microtime());
            $actual_time = $time_portions[1].substr($time_portions[0],1);
            $this->timers["$name"] = $actual_time;
        }

        function timerStop($name = 'default')
        {
            $time_portions = explode(' ',microtime());
            $actual_time = $time_portions[1].substr($time_portions[0],1);
            $elapsed_time = bcsub($actual_time, $this->timers["$name"], 6);
            return $elapsed_time;
        }
    }
    ?>
    Cheers
    Rob

    [Edit] Changed for loop to work down to first match. Fewer iterations required. I haven't tested this version though
    Last edited by OhLordy; 09-10-2003 at 11:59 AM.
    Bubble
    Honest, it is me. I'm a muppet! I've forgoten my passoword and my email address is my work one so I can't get it while I'm at home.

  5. #20
    black sheep with red wool Merve's Avatar
    Join Date
    Jul 2003
    Location
    North of the 49th parallel
    Posts
    2,193
    A little counter I made up...easy stuff...uses an external file...
    counter.txt holds the number of hits to the site...extremely simple stuff. This also uses a cookie so that you can't make the hits increment by just hitting the 'refresh' button over and over again.

    PHP Code:
    <?php
    $countercookie
    =$_COOKIE["countercookie"];
    $filename="counter.txt";
    $resource_handle=fopen($filename'r+');
    $size_of_file=filesize($filename);
    $number_of_hits=fread($resource_handle$size_of_file);
    if (!isset(
    $countercookie)) {
         @
    setcookie("countercookie","cookiefromme",time()+60*60*24,"/");
         
    $number_of_hits++;
    }
    ftruncate($resource_handle0);
    fwrite($resource_handle$number_of_hits);
    print 
    "This webpage currently has ".$number_of_hits." hits.";
    fclose($resource_handle);
    ?>
    Pretty simple eh? Use at will.

    [edit] made a correction...should truncate file to zero bytes [/edit]
    [edit2] made another correction...indenting and $number_of_hits++; [/edit2]
    Last edited by Merve; 10-02-2003 at 07:32 PM.
    "A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

    The Three C's

  6. #21
    Computers can do that? drawmack's Avatar
    Join Date
    Apr 2003
    Location
    Pocono Mtns PA
    Posts
    3,265
    so you can't turn register globals on with ini_set huh?

    well I don't like being told no so here goes

    PHP Code:
    <?php
    function reg_globals() {
        foreach(
    $_GET as $curVar) {
            global $
    $curVar;
            $
    $curVar $_GET[$curVar];
        } 
    //end foreach
        
    foreach($_POST as $curVar) {
            global $
    $curVar;
            $
    $curVar $_POST[$curVar];
        } 
    //end foreach
    //end reg_globals
    ?>

  7. #22
    Computers can do that? drawmack's Avatar
    Join Date
    Apr 2003
    Location
    Pocono Mtns PA
    Posts
    3,265
    here is a very simple template engine.

    PHP Code:
    function parse_template($filename) {
        
    $template_contents "";

        if(!
    file_exists($filename)) {die("Template ($filename) does note exist.");}
        if(!
    $fp fopen($filename,'r')) {die("Could not open template ($filename).");}

        
    $lines file($filename);

        
    $globals split(",",array_shift($lines));
        foreach (
    $globals as $gbl) {global $$gbl;}

        
    $template_contents implode("",$lines);

        
    $template_contents preg_replace("/\[(\w+)\]/e","\$\$1",$template_contents);

        return 
    $template_contents;
    //end parse_template 
    It takes a template in this format
    Var1,Var2,Var3
    the value of Var1 is [Var1]
    the value of Var2 is [Var2]
    the value of Var3 is [Var3]

    The first line is a list of the variables used in the template. The variable names do not include the $.

    Then each occurance of [VarName] is replaced with the value of $VarName in your script.

  8. #23
    Missing in Action LordShryku's Avatar
    Join Date
    Aug 2002
    Location
    Occupational Hypnotherapy
    Posts
    7,227
    Found a need this morning for this code. Had a class to parse a RSS feed from slashdot that worked fine from home, but wouldn't work behind my proxy at work. Sooo....needed to find a way around it. Here's a quick and dirty function to do that. Hope you guys can use it.
    PHP Code:
    <?
    function damnProxy($page, $proxyHost, $proxyPort) {
        $data = fsockopen($proxyHost, $proxyPort); 
        if(!$data)  {
            fclose($result); 
            echo "Cannot access ".$page;
        } 
        else { 
            fputs($data,"GET $page HTTP/1.0\n\n"); 
            while(!feof($data)) {
                $cont=$cont.fread($data,4096);
            }
        }
        fclose($data);
        return substr($cont,strpos($cont,"\r\n\r\n")+4);
    }

    $slash = damnProxy("http://slashdot.org/slashdot.rdf", "proxy.yourcompany.com", 8080);
    if($slash) {
        echo $slash;
    }
    else {
        echo "damned proxy";
    }
    ?>

  9. #24
    Member
    Join Date
    Mar 2003
    Posts
    67
    Originally posted by drawmack
    so you can't turn register globals on with ini_set huh?

    well I don't like being told no so here goes

    PHP Code:
    <?php
    function reg_globals() {
        foreach(
    $_GET as $curVar) {
            global $
    $curVar;
            $
    $curVar $_GET[$curVar];
        } 
    //end foreach
        
    foreach($_POST as $curVar) {
            global $
    $curVar;
            $
    $curVar $_POST[$curVar];
        } 
    //end foreach
    //end reg_globals
    ?>
    Well drawmack... you havent actually turned register globals on have you.

    I think it is time i shared some code which does the same sort of thing. Give me some feedback if you would.

    Thanks
    Option
    Last edited by Option; 09-15-2003 at 08:34 PM.

  10. #25
    Titles are overrated. Jeb.'s Avatar
    Join Date
    Jul 2003
    Posts
    150
    I hate to rain on people's parades, but:

    import_request_variables()

    Available since PHP 4.1.0.
    Once you eliminate the impossible, whatever remains, however improbable, must be the truth. - Sir Arthur Conan Doyle

  11. #26
    Computers can do that? drawmack's Avatar
    Join Date
    Apr 2003
    Location
    Pocono Mtns PA
    Posts
    3,265
    that's a cool function but I'm on a 4.0.3 server

  12. #27
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,572
    But wouldn't extract work for you, then (can't recall - it's been so long since I've been on 4.0.x)?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  13. #28
    black sheep with red wool Merve's Avatar
    Join Date
    Jul 2003
    Location
    North of the 49th parallel
    Posts
    2,193
    I started on 4.1.2 .... when it was out of date...and I'm still using 4.1.2!
    "A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

    The Three C's

  14. #29
    Better fan than rebelo! Moonglobe's Avatar
    Join Date
    Apr 2003
    Location
    brain://localhost:left-side
    Posts
    2,044
    well, ust for fun, here's a nice useless function:
    PHP Code:
    function do_nothing()
    {
        
    //do_nothing() version 5.2
        
    if ($args=func_get_args())
        {
            foreach (
    $args as $arg)
            {
                if (
    $arg && !($arg===1) && !($arg===true)) return false;
                else return 
    true;
            }
        }
        else return 
    true;


    [edit]<----post count = resolution height == very good day [/edit]
    there's no place i can be, since i found serenity.

  15. #30
    black sheep with red wool Merve's Avatar
    Join Date
    Jul 2003
    Location
    North of the 49th parallel
    Posts
    2,193
    Originally posted by Moonglobe
    well, ust for fun, here's a nice useless function:
    PHP Code:
    function do_nothing()
    {
        
    //do_nothing() version 5.2
        
    if ($args=func_get_args())
        {
            foreach (
    $args as $arg)
            {
                if (
    $arg && !($arg===1) && !($arg===true)) return false;
                else return 
    true;
            }
        }
        else return 
    true;


    [edit]<----post count = resolution height == very good day [/edit]
    I'm not starting a "Happy resolution height Moonglobe!" thread.
    "A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

    The Three C's

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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