Source codes, small scripts...

Collapse
This is a sticky topic.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #46
    bb code function:

    PHP Code:
    function bbcode($text)
    {
    // global $smilies;
             
    $text=htmlspecialchars($text);
             
    $text=preg_replace("/\[b\]\[big\](.*?)\[\/b\]\[\/big\]/i","[b]<big>\\1</big>[/b]"$text);
             
    $text=preg_replace("/\[big\]\[b\](.*?)\[\/big\]\[\/b\]/i","[b]<big>\\1</big>[/b]"$text);
             
    $text=preg_replace("/\[u\]\[big\](.*?)\[\/u\]\[\/big\]/i","[u]<big>\\1</big>[/u]"$text);
             
    $text=preg_replace("/\[big\]\[u\](.*?)\[\/big\]\[\/u\]/i","[u]<big>\\1</big>[/u]"$text);
             
    $text=preg_replace("/\[b\]\[u\](.*?)\[\/b\]\[\/u\]/i","[b][u]\\1[/u][/b]"$text);
             
    $text=preg_replace("/\[u\]\[b\](.*?)\[\/u\]\[\/b\]/i","[b][u]\\1[/u][/b]"$text);
             
    $text=preg_replace("/\[b\]\[i\](.*?)\[\/b\]\[\/i\]/i","[b][i]\\1[/i][/b]"$text);
             
    $text=preg_replace("/\[i\]\[b\](.*?)\[\/i\]\[\/b\]/i","[b][i]\\1[/i][/b]"$text);
             
    $text=preg_replace("/\[b\]\[u\](.*?)\[\/b\]\[\/u\]/i","[b][u]\\1[/u][/b]"$text);
             
    $text=preg_replace("/\[u\]\[b\](.*?)\[\/u\]\[\/b\]/i","[b][u]\\1[/u][/b]"$text);
             
    $text=preg_replace("/\[b\]\[small\](.*?)\[\/b\]\[\/small\]/i","[size="1"][b]\\1[/b][/size]"$text);
             
    $text=preg_replace("/\[small\]\[b\](.*?)\[\/small\]\[\/b\]/i","[size="1"][b]\\1[/b][/size]"$text);
             
    $text=preg_replace("/\[i\]\[small\](.*?)\[\/i\]\[\/small\]/i","[size="1"][i]\\1[/i][/size]"$text);
             
    $text=preg_replace("/\[small\]\[i\](.*?)\[\/small\]\[\/i\]/i","[size="1"][i]\\1[/i][/size]"$text);
             
    $text=preg_replace("/\[u\]\[small\](.*?)\[\/u\]\[\/small\]/i","[size="1"][u]\\1[/u][/size]"$text);
             
    $text=preg_replace("/\[small\]\[u\](.*?)\[\/small\]\[\/u\]/i","[size="1"][u]\\1[/u][/size]"$text);
             
    $text=preg_replace("/\[b\]\[big\](.*?)\[\/b\]\[\/big\]/i","<big>[b]\\1[/b]</big>"$text);
             
    $text=preg_replace("/\[big\]\[b\](.*?)\[\/big\]\[\/b\]/i","<big>[b]\\1[/b]</big>"$text);
             
    $text=preg_replace("/\[i\]\[big\](.*?)\[\/i\]\[\/big\]/i","<big>[i]\\1[/i]</big>"$text);
             
    $text=preg_replace("/\[big\]\[i\](.*?)\[\/big\]\[\/i\]/i","<big>[i]\\1[/i]</big>"$text);
             
    $text=preg_replace("/\[u\]\[big\](.*?)\[\/u\]\[\/big\]/i","<big>[u]\\1[/u]</big>"$text);
             
    $text=preg_replace("/\[big\]\[u\](.*?)\[\/big\]\[\/u\]/i","<big>[u]\\1[/u]</big>"$text);
             
    $text=preg_replace("/\[b\](.*?)\[\/b\]/i","[b]\\1[/b]"$text);
             
    $text=preg_replace("/\[u\](.*?)\[\/u\]/i","[u]\\1[/u]"$text);
             
    $text=preg_replace("/\[i\](.*?)\[\/i\]/i","[i]\\1[/i]"$text);
             
    $text=preg_replace("/\[big\](.*?)\[\/big\]/i","<big>\\1</big>"$text);
             
    $text=preg_replace("/\[small\](.*?)\[\/small\]/i","[size="1"]\\1[/size]"$text);
             
    $text=preg_replace("/\[br\/\]/i","
    "
    $text);
             
    $text=preg_replace("/\[url\](.*?)\[\/url\]/i","<a href=\"\\1\">\\1</a>"$text);
             
    $text=preg_replace("/\[link\=(.*?)\](.*?)\[\/link\]/i","<a href=\"\\1\">\\2</a>"$text);
             
    $text=preg_replace("/\[img\=((.*?)(.jpg|jpeg))\]/i","<a href=\"\\1\"><img src=\"\\1\" alt=\"***\"/></a>"$text);
             
    $text eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','[email="\\1"]\\1[/email]'$text);

    Advertise your mobile site for FREE with AdTwirl

    Comment


      #47
      function to get members age :

      PHP Code:
      ////////////////////////////GET USERS AGE//////////////////////////////////
      function getage($birthdate)
      {
         list(
      $day$month$year) = split('-',  $birthdate);
                    
      $tmonth date('n');
                    
      $tday date('j');
                    
      $tyear date('Y');
                    
      $years $tyear $year;
                    if (
      $tmonth <= $month)
                    {
                    if (
      $month == $tmonth)
                    {
                    if (
      $day $tday)$years--;
                     }
                     else 
      $years--;
                     }

                 return 
      $years;

      usage

      Code:
      echo getage("30-05-1982");
      Advertise your mobile site for FREE with AdTwirl

      Comment


        #48
        Function to cut html tags from a text, can be used to remove html tags from rss news

        PHP Code:
        function theRealStripTags($string)
        {
            
        //while there are tags left to remove
            
        while(strstr($string'>'))
            {
                
        //find position of first carrot
                
        $currentBeg strpos($string'<');

                
        //find position of end carrot
                
        $currentEnd strpos($string'>');

                
        //find out if there is string before first carrot
                //if so save it in $tmpstring
                
        $tmpStringBeg = @substr($string0$currentBeg);

                
        //find out if there is string after last carrot
                //if so save it in $tmpStringEnd
                
        $tmpStringEnd = @substr($string$currentEnd 1strlen($string));

                
        //cut the tag from the string
                
        $string $tmpStringBeg.$tmpStringEnd;
            }

            return 
        $string;

        Advertise your mobile site for FREE with AdTwirl

        Comment


          #49
          It displays a random quote on a web page. Quotes are picked from a list that you define.
          PHP Code:
          <?php 

          /** 
           * Add this line of code in your page: 
           * <?php include "random_quote.php"; ?> 
           */ 

          $quotes[] = "This is a quote"
          $quotes[] = "This is another"
          $quotes[] = "quote 3"
          $quotes[] = "quote 4"
          $quotes[] = "quote 5"
          $quotes[] = "quote 6"

          srand ((double) microtime() * 1000000); 
          $randomquote rand(0,count($quotes)-1); 

          echo 
          "

          $quotes[$randomquote] . "</p>"

          ?>

          Comment


            #50
            It allows visitors to your site to read your guestbook entries and post a message of their own. Very simple setup, only requires you to change 4 settings. Uses MySQL to store the entries.
            PHP Code:
            <?php 

            /** 
             * Create the table in your MySQL database: 
             *  
             * CREATE TABLE guests ( 
             *   id int(10) NOT NULL auto_increment, 
             *   name varchar(50) NOT NULL, 
             *   message varchar(255) NOT NULL, 
             *   date timestamp(14) NOT NULL, 
             *   PRIMARY KEY (id) 
             * ) 
             *  
             * Change the database login settings to your own 
             *  
             * The script is now ready to run 
             */ 

            // Change these to your own database settings 
            $host "localhost"
            $user "username"
            $pass "password"
            $db "database"

            mysql_connect($host$user$pass) OR die ("Could not connect to the server."); 
            mysql_select_db($db) OR die("Could not connect to the database."); 
                   
            $name stripslashes($_POST['txtName']); 
            $message stripslashes($_POST['txtMessage']); 

            if (!isset(
            $_POST['txtName'])) { 

                
            $query "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y @ %H:%i') as newdate FROM guests ORDER BY id DESC"
                
            $result mysql_query($query); 
                 
                while (
            $row mysql_fetch_object($result)) { 

            ?> 



            <?php echo $row->message?> 

            Posted by <?php echo $row->name?> on <?php echo $row->newdate?></p> 

            <?php 
                     
                

                 
            ?> 



            Post a message</p> 

            <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> 

                

            <label for="txtName">Name:</label>
             
                <input type="text" title="Enter your name" name="txtName" /></p> 

                

            <label for="txtMessage">Your message:</label>
             
                <textarea title="Enter your message" name="txtMessage"></textarea></p> 
                 
                

            <label title="Send your message"> 
                <input type="submit" value="Send" /></label></p> 
                 
            </form> 

            <?php 



            else { 

                
            // Adds the new entry to the database 
                
            $query "INSERT INTO guests SET message='$message', name='$name', date=NOW()"
                
            $result mysql_query($query); 

                
            // Takes us back to the entries 
                
            $ref $_SERVER['HTTP_REFERER']; 
                
            header ("Location: $ref"); 


            ?>

            Comment


              #51
              hit counter
              PHP Code:
              <?php 

              /** 
               * Create an empty text file called counterlog.txt ,Chmod 777 it and**
               * upload to the same directory as the page you want to**
               * count hits for. 
               ***
               * Add this line of code on your page: 
               * <?php include "text_file_hit_counter.php"; ?> 
               */ 

              // Open the file for reading 
              $fp fopen("counterlog.txt""r"); 

              // Get the existing count 
              $count fread($fp1024); 

              // Close the file 
              fclose($fp); 

              // Add 1 to the existing count 
              $count $count 1

              // Display the number of hits 
              // If you don't want to display it, comment out this line 
              echo "

              Page views:" 
              $count "</p>"

              // Reopen the file and erase the contents 
              $fp fopen("counterlog.txt""w"); 

              // Write the new count to the file 
              fwrite($fp$count); 

              // Close the file 
              fclose($fp); 

              ?>

              Comment


                #52
                It displays a hyperlinked list of all the files contained in a specified folder. A really simple way to have your own directory browser, just drop the file in the folder and it will show an index of all files in the directory on your webspace.
                PHP Code:
                <? 

                /** 
                 * Change the path to your folder. 
                 * 
                 * This must be the full path from the root of your 
                 * web space. If you're not sure what it is, ask your host. 
                 * 
                 * Name this file index.php and place in the directory. 
                 */ 

                    // Define the full path to your folder from root 
                    $path = "/home/user/public/foldername/"; 

                    // Open the folder 
                    $dir_handle = @opendir($path) or die("Unable to open $path"); 

                    // Loop through the files 
                    while ($file = readdir($dir_handle)) { 

                    if($file == "." || $file == ".." || $file == "index.php" ) 

                        continue; 
                        echo "<a href=\"$file\">$file</a>
                "; 

                    } 

                    // Close 
                    closedir($dir_handle); 

                ?>

                Comment


                  #53
                  A simple form mail PHP script that displays a contact form to allow visitors to your site to send you a message via email. Built in security prevents spammers hijacking it from another domain.
                  PHP Code:
                  <?php 

                  /** 
                   * Change the email address to your own. 
                   * 
                   * $empty_fields_message and $thankyou_message can be changed 
                   * if you wish. 
                   */ 

                  // Change to your own email address 
                  $your_email "you@example.com"

                  // This is what is displayed in the email subject line 
                  // Change it if you want 
                  $subject "Message via your contact form"

                  // This is displayed if all the fields are not filled in 
                  $empty_fields_message "

                  Please go back and complete all the fields in the form.</p>"


                  // This is displayed when the email has been sent 
                  $thankyou_message "

                  Thankyou. Your message has been sent.</p>"


                  // You do not need to edit below this line 

                  $name stripslashes($_POST['txtName']); 
                  $email stripslashes($_POST['txtEmail']); 
                  $message stripslashes($_POST['txtMessage']); 

                  if (!isset(
                  $_POST['txtName'])) { 

                  ?> 

                  <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> 

                      

                  <label for="txtName">Name:</label>
                   
                      <input type="text" title="Enter your name" name="txtName" /></p> 

                      

                  <label for="txtEmail">Email:</label>
                   
                      <input type="text" title="Enter your email address" name="txtEmail" /></p> 

                      

                  <label for="txtMessage">Your message:</label>
                   
                      <textarea title="Enter your message" name="txtMessage"></textarea></p> 

                      

                  <label title="Send your message"> 
                      <input type="submit" value="Send" /></label></p> 

                  </form> 

                  <?php 



                  elseif (empty(
                  $name) || empty($email) || empty($message)) { 

                      echo 
                  $empty_fields_message



                  else { 

                      
                  // Stop the form being used from an external URL 
                      // Get the referring URL 
                      
                  $referer $_SERVER['HTTP_REFERER']; 
                      
                  // Get the URL of this page 
                      
                  $this_url "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]; 
                      
                  // If the referring URL and the URL of this page don't match then 
                      // display a message and don't send the email. 
                      
                  if ($referer != $this_url) { 
                          echo 
                  "You do not have permission to use this script from another URL."
                          exit; 
                      } 

                      
                  // The URLs matched so send the email 
                      
                  mail($your_email$subject$message"From: $name <$email>"); 

                      
                  // Display the thankyou message 
                      
                  echo $thankyou_message
                       


                  ?>

                  Comment


                    #54
                    Detect mobile os:


                    PHP Code:
                    function detect_os ($ua) {
                            if (
                    preg_match('#PalmOS#i'$ua)) {
                                    
                    $os_name "Palm OS";
                                    
                    $os_code "palm";
                            } elseif (
                    preg_match('#Windows CE#i'$ua)) {
                                    
                    $os_name "Windows CE";
                                    
                    $os_code "windows";
                            } elseif (
                    preg_match('#QtEmbedded#i'$ua)) {
                                    
                    $os_name "Qtopia";
                                    
                    $os_code "linux";
                            } elseif (
                    preg_match('#Zaurus#i'$ua)) {
                                    
                    $os_name "Linux";
                                    
                    $os_code "linux";
                            } elseif (
                    preg_match('#Symbian#i'$ua)) {
                                    
                    $os_name "Symbian OS";
                                    
                    $os_code "symbian";
                            }

                            if (
                    preg_match('#PalmOS/sony/model#i'$ua)) {
                                    
                    $pda_name "Sony Clie";
                                    
                    $pda_code "sony";
                            } elseif (
                    preg_match('#Zaurus ([a-zA-Z0-9.]+)#i'$ua$matches)) {
                                    
                    $pda_name "Sharp Zaurus " $matches[1];
                                    
                    $pda_code "zaurus";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#Series ([0-9]+)#i'$ua$matches)) {
                                    
                    $pda_name "Series";
                                    
                    $pda_code "nokia";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#Nokia ([0-9]+)#i'$ua$matches)) {
                                    
                    $pda_name "Nokia";
                                    
                    $pda_code "nokia";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#SIE-([a-zA-Z0-9]+)#i'$ua$matches)) {
                                    
                    $pda_name "Siemens";
                                    
                    $pda_code "siemens";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#dopod([a-zA-Z0-9]+)#i'$ua$matches)) {
                                    
                    $pda_name "Dopod";
                                    
                    $pda_code "dopod";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#o2 xda ([a-zA-Z0-9 ]+);#i'$ua$matches)) {
                                    
                    $pda_name "O2 XDA";
                                    
                    $pda_code "o2";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#SEC-([a-zA-Z0-9]+)#i'$ua$matches)) {
                                    
                    $pda_name "Samsung";
                                    
                    $pda_code "samsung";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#SonyEricsson ?([a-zA-Z0-9]+)#i'$ua$matches)) {
                                    
                    $pda_name "SonyEricsson";
                                    
                    $pda_code "sonyericsson";
                                    
                    $pda_ver $matches[1];
                            } elseif (
                    preg_match('#Wii#i'$ua$matches)) {
                                    
                    $pda_name "Nintendo Wii";
                                    
                    $pda_code "wii";
                            }
                            return array(
                    $os_name$os_code$os_ver$pda_name$pda_code$pda_ver);

                    usage:

                    PHP Code:
                    $info=detect_os ($_SERVER['HTTP_USER_AGENT']);
                    echo 
                    'OS name: '.$info[0].'
                    '
                    ;
                    echo 
                    'OS code: '.$info[1].'
                    '
                    ;
                    echo 
                    'OS version: '.$info[2].'
                    '
                    ;
                    echo 
                    'Brand: '.$info[3].'
                    '
                    ;
                    //echo 'Phone code: '.$info[4].'
                    ';
                    echo '
                    Model'.$info[5].'
                    '; 
                    this function I took from counterize mod for wordpress (_http://wordpress.org/extend/plugins/counterizeii/) but its also useful for your mobile sites
                    Advertise your mobile site for FREE with AdTwirl

                    Comment


                      #55
                      Near begining of index.php find $brws line and change to this

                      PHP Code:
                      $brws explode(" ",$_SERVER['HTTP_USER_AGENT']);
                      $ubr $brws[0];
                      $uip getip();
                      $action $_GET["action"];
                      $sid $_GET["sid"];
                      $page $_GET["page"];
                      $who $_GET["who"]; 
                      It works at my place and detect Opera,Nokia,Samsung ... and their versions too (:

                      Comment


                        #56
                        Ban IP range:

                        PHP Code:
                        $getip $_SERVER["REMOTE_ADDR"];
                        $banned_ip = array();
                        $banned_ip[] = '194.9.94.*';
                        $banned_ip[] = '77.105.2.*';


                        foreach(
                        $banned_ip as $banned)
                        {
                        $blacked=str_replace('*'''$banned);
                        $len=strlen($blacked);
                        if (
                        $ip==$blacked || substr($getip0$len)==$blacked)
                        {
                        echo 
                        'You are banned!';
                        exit;
                        }

                        Advertise your mobile site for FREE with AdTwirl

                        Comment


                          #57
                          Unique Hitcounter
                          PHP Code:
                          <?php

                            $ip
                          =$_SERVER[REMOTE_ADDR];

                            
                          $file=file_get_contents("ipLog.txt");

                            if(
                          substr_count($file$ip)==0) {

                            
                          $file.="\n" $ip;

                            }

                            
                          file_put_contents("ipLog.txt"$file);

                            
                          $total=substr_count($file"\n");
                          echo 
                          "$total";

                          ?>
                          make a ipLog.txt in the same directory and chmod777

                          Comment


                            #58
                            Code:
                            $start = microtime(true);
                            sleep(1);
                            $end = microtime(true);
                            printf("Time spent: %f", ($end - $start));

                            Comment


                              #59
                              Siple WML guestbook
                              Create a WML Form to Collect user's comments, name and email address. Save following code as Guestbook.wml

                              PHP Code:
                              <?xml version="1.0"?>
                                  <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.3//EN" "http://www.wapforum.org/DTD/wml13.dtd" >
                                  <wml>
                                  <card>
                                  <p align="center">Guest Book!</p>

                                  Your Name : <input type="text" name="user_name"/>

                                  Your Email: <input type="text" name="user_email"/>

                                  Your Comments : <input type="text" name="user_comments"/>

                                  <anchor>
                                  <go href="save_comments.php" method="post">
                                  <postfield name="user_name" value="$user_name"/>
                                  <postfield name="user_email" value="$user_email"/>
                                  <postfield name="user_commetns" value="$user_comments"/>
                                  </go>
                                  Please Click Here To SEND
                                  </anchor>
                                  </card>
                                  </wml> 
                                  </FONT> And now create an-other file. This file contains PHP code to Create text file (user_comments.txt) in C:\. User_comments.txt file will hold all data submit by Users. 
                                   (Note : Save following file as save_comments.php and don't forget to save this both files in same directory) 
                                    
                                  <FONT FACE="Courier New" COLOR="#008000"> /* Before Commercial use don't forget to ask me [email]yadish_sahar@hotmail.com[/email]</FONT><FONT FACE="Courier New" COLOR="#008000"> */ 
                                  <? header("Content-type:text/vnd.wap.wml"); 
                                  if (isset($user_name) and isset($user_email) 
                                  and isset($user_comments) and ($user_name and $user_email and $user_comments != "")) 
                                  { 
                                  $file_name="c:\user_comments.txt"; 
                                  $file_data = fopen($file_name ,"a"); 
                                  $data_to_write = "User Name is :".$user_name."\n". 
                                  "Email Address is :".$user_email."\n". 
                                  "He/She Submit this Comments :".$user_comments."\n". 
                                  "*************************\n\n"; 
                                  fwrite($file_data,$data_to_write); 
                                  fclose($file_data); 
                                  echo "<?xml version=\"1.0\"?> 
                                  <!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\" \"http://www.wapforum.org/DTD/wml13.dtd\" > 
                                  <wml> 
                                  <card> 
                                  <p align=\"center\">Thank You!</p>
                               
                                  Your Comments is Saved. Thank you very much again 
                                  </card></wml>"; 
                                  } 
                                  else 
                                  { 
                                  Echo "<?xml version=\"1.0\"?> 
                                  <!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.3//EN\" \"http://www.wapforum.org/DTD/wml13.dtd\" > 
                                  <wml> 
                                  <card> 
                                  

                              Some thing is Wrong with Your Form Submission!  
                                  </p> 
                                  </card></wml>"; 
                                  } 
                                  ?>

                              Comment


                                #60
                                get country by ip address, you need url_fopen enabled

                                PHP Code:
                                <?php

                                /**
                                 * @author Milomir
                                 * @copyright 2008
                                 */

                                function GetCountry($IpAddr){
                                $APIUrl "http://api.hostip.info/country.php?ip=" $IpAdds "";
                                $GetCID fopen($APIUrl"r");
                                while(
                                $CIData fread($GetCID1024)){
                                **
                                $ReadCID .= $CIData;
                                }
                                if((
                                $ReadCID == "YU") or ($ReadCID == "RS")){
                                **
                                $Country "Serbia";
                                } elseif(
                                $ReadCID == "US"){
                                **
                                $Country "United States";
                                } else {
                                **
                                $Country "Unknown";
                                }
                                fclose($GetCID);

                                return 
                                $Country;
                                }

                                ?>
                                usage
                                PHP Code:
                                <?php
                                echo "Ip: " $_SERVER["REMOTE_ADDR"] . 
                                \n"
                                ;
                                echo 
                                "Country: " GetCountry($_SERVER["REMOTE_ADDR"]) . "\n";
                                ?>
                                i have added only two countrys to this, you need to add others by yourself, follow my example..

                                Comment

                                Working...
                                X