newsblog
Fri, 24 Feb 2017, 19:05 *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Hi gusy!
 
   Home   Help Search Login Register  
Pages: 1 [2] 3   Go Down
  Print  
Author Topic: PHP SNIPPETS  (Read 10065 times)
0 Members and 1 Guest are viewing this topic.
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #15 on: Thu, 24 Jan 2008, 22:23 »

upload on website then download is a waste of time.
unless that site is on my computer tongue
Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #16 on: Mon, 08 Sep 2008, 18:07 »

random image
(check my sig)
Imaginile trebuie sa fie in acelasi director cu php-ul si sa fie denumite in genul: 1.gif, 2.jpg, 3.png, etc.
usage:
[ img ] http://wooptoo.com/fortune/index.php [ /img ]

Script-ul e scris de Smarty din cate imi amintesc.

Code:
<?php
class Icon {
var $currentDir;
var $fileTypes = Array();
var $icons = Array();

function Icon$dir ) {
$this->currentDir $dir;
mt_srand ((double) microtime() * 1000000);
}

function setDir $dir ) {
$this->currentDir $dir;
}

function getDir ( ) {
if ( empty($this->currentDir) ) {
exit ( "geen directory is gespecificeerd" );
} else {
return $this->currentDir;
}
}

function addFileType $t ) {
if (!in_array$t$this->fileTypes )) {
array_push($this->fileTypes$t);
}
}

function removeFileType $t ) {
for ( $i 0$i count$this->fileTypes ); $i++ ) {
if ( $this->fileTypes[$i] == $t ) {
array_splice($this->fileTypes,$i,1);
}
}
}

function checkIfExistFileType $t ) {
if (in_array$t$this->fileTypes )) {
return TRUE;
} else {
return FALSE;
}
}

function getFileExtension $f ) {
return strtolower(substr($str substr($f, ($pos strrpos($f'/')) !== false ? ++$pos 0), strpos($str'.') + 1));
}

function makeIconList() {
if ( empty($this->currentDir) ) {
exit ( "geen directory is gespecificeerd" );
} else {
$d dir($this->currentDir);
}

while (false !== ($entry $d->read())) {

$ext $this->getFileExtension $entry );
if ( $entry != "." || $entry != ".." ) {
if ( $this->checkIfExistFileType($ext) ) {
$this->icons[] = $entry;
}
}
}
}

function getRandomIconString() {
$iconNr mt_rand(0,count($this->icons)-1);
return $this->icons[$iconNr];
}

function getRandomIconFile() {
$iconNr mt_rand(0,count($this->icons)-1);

Header("Pragma: no-cache");
Header("Cache-Control: no-cache, must-revalidate");

switch ( $this->getFileExtension$this->icons[$iconNr] )) {
case "gif":
Header("Content-type: image/gif");
break;
case "jpg":
Header("Content-type: image/jpeg");
break;
case "swf":
Header("Content-type: application/x-shockwave-flash");
break;
case "png":
Header("Content-type: image/png");
break;
default:
Header("Content-Type: application/octet-stream");
Header("Content-Disposition: attachment; filename=" $this->icons[$iconNr] . "");
break;
}

readfile($this->icons[$iconNr]);
}

}

$i = new Icon"." );

$i->addFileType"gif" );
$i->addFileType"jpg" );
$i->addFileType"swf" );
$i->addFileType"png" );
$i->makeIconList();

echo 
$i->getRandomIconFile();

?>


Logged
wooptoo yoshi: da, asa l-a facut. cred ca asta il foloseai si tu pe J forum pt random avatar
yoshi
fap -> ∞
Coșcojemete

Offline Offline

Gender: Male
Posts: 10 684


| (• ◡•)|/ \(❍ᴥ❍ʋ)


View Profile
« Reply #19 on: Tue, 09 Sep 2008, 01:00 »

yoshi: da, asa l-a facut. cred ca asta il foloseai si tu pe J forum pt random avatar
no, eu aveam ceva facut de smrty sau stokky
Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #20 on: Tue, 09 Sep 2008, 01:22 »

no, eu aveam ceva facut de smrty sau stokky

Script-ul e scris de Smarty din cate imi amintesc.

pay attention tongue
asta era pentru ca de la tine il am LoL
Logged
yoshi
fap -> ∞
Coșcojemete

Offline Offline

Gender: Male
Posts: 10 684


| (• ◡•)|/ \(❍ᴥ❍ʋ)


View Profile
« Reply #21 on: Tue, 09 Sep 2008, 03:55 »

pay attention tongue
asta era pentru ca de la tine il am LoL
urr.....right grin my bad grin
Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #22 on: Fri, 01 May 2009, 23:37 »

Some stuff i wrote:

Code:
<?php
/**
 * Get the GET string of the current page.
 * This is used by forms to stay on the same page after submitting.
 *
 * @param bool $escape - whether to escape ampersands or not
 * @return string $get_string - complete GET string
 */
function current_page($escape) {
if($escape) {
$amp '&amp;';
} else {
$amp '&';
}
$keys array_keys($_GET);
$values array_values($_GET);
$count count($_GET);
$get_string '?';
for ($i=0$i<$count$i++) {
$get_string $get_string $keys[$i] . '=' $values[$i];
if ($i<$count-1) {
$get_string $get_string $amp;
}
}
if ($get_string == '?') {
$get_string '.';
}
return $get_string;
}
?>

Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #23 on: Mon, 04 May 2009, 15:21 »

Wooptoo, n-am vazut mai devreme, dar:

http://us2.php.net/manual/ro/function.http-build-query.php

Code:
<?php
$data 
= array('foo'=>'bar',
              
'baz'=>'boom',
              
'cow'=>'milk',
              
'php'=>'hypertext processor');

echo 
http_build_query($data); // foo=bar&baz=boom&cow=milk&php=hypertext+processor
echo http_build_query($data'''&amp;'); // foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor

?>

Logged
wooptoo Da, intre timp am gasit si eu functia aia smile
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #25 on: Sat, 03 Oct 2009, 22:47 »

client de IMAP in PHP:

Code:
<?php
/**
 * http://www.devarticles.com/c/a/PHP/Create-Your-Own-Mail-Script-With-PHP-and-IMAP/
 *
    * imap_open: Opens an IMAP stream to a mailbox.
    * imap_headers: Returns headers for all emails in a mailbox.
    * imap_headerinfo: Reads the header of an email.
    * imap_delete: Mark an email for deletion from current mailbox.
    * imap_expunge: Delete all emails marked for deletion.
    * imap_mail: Sends an email message to a recipient.
    * imap_body: Reads the body of an email from a mailbox.
 */

$server 'imap.gmail.com';
$user '';
$pass '';
$param '/notls/norsh/novalidate-cert/ssl';
$conn imap_open('{'.$server.$param.'}Inbox'$user$pass);

$headers imap_headers($conn);
$numEmails sizeof($headers);


for (
$i 1$i $numEmails+1$i++) {
$mailHeader imap_headerinfo($conn$i);
$from $mailHeader->fromaddress;
$subject $mailHeader->subject;
$date $mailHeader->date;

echo "Email from $from, subject $subject, date $date";
echo "\n";

echo imap_body($conn$i);
echo "\n";
}

imap_close($conn);
?>


Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #26 on: Sun, 25 Oct 2009, 23:40 »

Completare la codul de mai sus, in ciclul for:

Code:
<?php
// to get content-type (and boundary) line:
$full_headers=imap_fetchheader($conn$i);
preg_match('/\nContent-Type.*/i'$full_headers$match);
$content_type $match[0];
echo 
$content_type;
?>

Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #27 on: Tue, 27 Oct 2009, 02:46 »

Client SMTP scris in PHP. Awesome. Foloseste smtp-ul gmail pentru a trimite mailuri.

Code:
<?php

function authgMail($from$namefrom$to$nameto$subject$message) {


$smtpServer "smtp.gmail.com";   //ip address of the mail server.  This can also be the local domain name. Add ssl:// for SSL and set $tls to false
$port "25";                     // 25 by default
$timeout "45";                 // typical timeout. try 45 for slow servers
$username ""// the login for your smtp
$password "";            // the password for your smtp
$localhost "127.0.0.1";       // Defined for the web server.
$newLine "\r\n";             // aka, carrage return line feed. var just for newlines in MS
$tls true;                  // change to 1 if your server is running under TLS





//connect to the host and port
$smtpConnect fsockopen($smtpServer$port$errno$errstr$timeout);
$smtpResponse fgets($smtpConnect4096);

if(empty(
$smtpConnect)) {
   
$output "Failed to connect: $smtpResponse";
   echo 
$output;
   return 
$output;
} else {
   
$logArray['connection'] = "<p>Connected to: $smtpResponse";
   echo 
"<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}

//you have to say HELO again after TLS is started
   
fputs($smtpConnect"HELO $localhost"$newLine);
   
$smtpResponse fgets($smtpConnect4096);
   
$logArray['heloresponse2'] = "$smtpResponse";

if (
$tls) {

   
fputs($smtpConnect"STARTTLS"$newLine);
   
$smtpResponse fgets($smtpConnect4096);
   
$logArray['heloresponse2'] = "$smtpResponse";

    
stream_socket_enable_crypto($smtpConnecttrueSTREAM_CRYPTO_METHOD_TLS_CLIENT);

    
//Say HELO again after TLS is started
   
fputs($smtpConnect"HELO $localhost"$newLine);
   
$smtpResponse fgets($smtpConnect4096);
   
$logArray['heloresponse2'] = "$smtpResponse";

}


//request for auth login
fputs($smtpConnect,"AUTH LOGIN" $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['authrequest'] = "$smtpResponse";

//send the username
fputs($smtpConnectbase64_encode($username) . $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnectbase64_encode($password) . $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['authpassword'] = "$smtpResponse";

//email from
fputs($smtpConnect"MAIL FROM: <$from>" $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['mailfromresponse'] = "$smtpResponse";

//email to
fputs($smtpConnect"RCPT TO: <$to>" $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['mailtoresponse'] = "$smtpResponse";

//the email
fputs($smtpConnect"DATA" $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['data1response'] = "$smtpResponse";

//construct headers
$headers "MIME-Version: 1.0" $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" $newLine;
$headers .= "To: $nameto <$to>" $newLine;
$headers .= "From: $namefrom <$from>" $newLine;

//observe the . after the newline, it signals the end of message
fputs($smtpConnect"To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse fgets($smtpConnect4096);
$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" $newLine);
$smtpResponse fgets($smtpConnect4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);

}


// MAIL

  
$from="x@gmail.com";
  
$namefrom="";
  
$to "y@example.org";
  
$nameto "";
  
$subject "";
  
$message "";
  
// this is it, lets send that email!
  
$x authgMail($from$namefrom$to$nameto$subject$message);




echo 
'<pre>';
var_dump($x);
echo 
'</pre>';



?>


« Last Edit: Tue, 27 Oct 2009, 17:54 by wooptoo » Logged
punkt
personal space invader
Baistruc

Offline Offline

Gender: Male
Posts: 1 458



View Profile
« Reply #28 on: Tue, 12 Jan 2010, 09:45 »

Requirements: PHP 5, doesn't work in PHP 4 afaik.

Code:
<?php
class Dog
{
   var 
$name=null;
   var 
$race=null;
   var 
$age=null;

   function 
name($string)
   {
       
$this->name=$string;
       return 
$this;
   }

   function 
age($integer)
   {
       
$this->age=$integer;
       return 
$this;
   }

   function 
race($string)
   {
       
$this->rage=$string;
       return 
$this;
   }

   function 
bark()
   {
   echo 
"Hello, my name is {$this->name}, i am a {$this->race} and I am {$this->age} year(s) old";
   }
}

$pet = new Dog;

$pet->name("Bart")->race("Yorkshire Terrier")->age(2)->bark();
 
 
//or:

$pet
     
->name("Bart")
     ->
race("Yorkie")
     ->
age(2)
->
bark();

?>


Will Output:

Quote
Hello, my name is Bart, i am a Yorkshire Terrier and I am 2 year(s) old

Folosesc metoda asta in conjuctie cu functiile de databases ale CodeIgniter, la modele, cu ceva functii automagice ('__call','__get','__set), la ceva de genul:

$user = $this->User->name('john')->join('details','details.owner=users.id')->limit(1)->fetch();

SQL: "SELECT * FROM `users` JOIN `details` on `details`.`owner` = `users`.`id` WHERE `users.name`=`john` LIMIT 1"
Logged
wooptoo
Baistruc

Offline Offline

Posts: 6 199


◕ ◡ ◕


View Profile WWW
« Reply #29 on: Wed, 28 Apr 2010, 23:39 »

http://net.tutsplus.com/tutorials/php/9-useful-php-functions-and-features-you-need-to-know

De glob() nu stiam si ma chinuiam cu scandir()...
Logged
Pages: 1 [2] 3   Go Up
  Print  
 
Jump to:  

Designed by | Licensed under Creative Commons BY-NC-ND 3.0 | Powered by SMF 1.1.21 | SMF © 2015, Simple Machines