Pages

Monday, December 31, 2012

Error types in PHP

PHP has a number of possible errors that it might return, all of which mean something different and are treated differently. Here is the complete list:
E_ERROR Fatal run-time error. Script execution is terminated because the error cannot be recovered from.
E_WARNING Run-time warning. Execution of the script is not terminated because the situation can be recovered from.
E_PARSE Compile-time parse errors. Only generated by the PHP parser.
E_NOTICE Run-time notice. Execution of the script is not terminated, but it is possible there is an error in your code.
E_CORE_ERROR Fatal error in PHP’s internals. Indicates a serious problem with your PHP installation.
E_CORE_WARNING Compile-time warning. Generally indicates a problem with your PHP installation.
E_COMPILE_ERROR Fatal compile-time error. This indicates a syntax error in your script that could not be recovered from.
E_COMPILE_WARNING This indicates a non-fatal syntax error in your script
E_USER_ERROR User-generated error message. This is generated from inside PHP scripts to halt execution with an appropriate message.
E_USER_WARNING User-generated warning message. This is generated from inside PHP scripts to flag up a serious warning message without halting execution.
E_USER_NOTICE User-generated notice message. This is generated from inside PHP scripts to print a minor notice to the screen, usually regarding potential problems with scripts.
E_ALL This is a catch-all error type, which means “all errors combined”.
All warnings and notices can usually be recovered from without too much problem, however errors are critical and usually mean “you would not want to recover from this”.
User errors, user warnings, and user notices are all generated using the trigger_error() function, and you should use them in your own code to handle possible errors that others (or indeed you) might make when calling your own functions.
Notices are generally very minor things – using an uninitialized variable, for example – that may be a sign that you have got a hidden bug lurking in there, but it may also be there by design, as notices are generally quite strict.

Wednesday, December 26, 2012

PHP SMTP Mail - [Resolved]

<?php

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

$smtpServer = "smtpout.secureserver.net";   //ip address of the mail server.  This can also be the local domain name
$port = "25";                     // should be 25 by default, but needs to be whichever port the mail server will be using for smtp
$timeout = "45";                 // typical timeout. try 45 for slow servers
$username = "admin@dmin.com"; // the login for your smtp
$password = "password";            // the password for your smtp
$localhost = "localhost";       // Defined for the web server.  Since this is where we are gathering the details for the email
$newLine = "\r\n";             // aka, carrage return line feed. var just for newlines in MS
$secure = 0;                  // change to 1 if your server is running under SSL

$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
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($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";

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

//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";

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

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

//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$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($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

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

  $from="admin@admin.com";
  $namefrom="Internal Sales Dept. of mydomain.com";
  $to = "tomail@admin.com";
  $nameto = "internal_user";
  $subject = "Email from My Domain";
  $message = "Email from My Domain";
  // this is it, lets send that email!
  authgMail($from, $namefrom, $to, $nameto, $subject, $message);


?>

How to implement a link scraper in PHP? - [resolved]


$html = file_get_contents("http://example.com");

$dom = new DOMDocument();
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

$list_urls = array();
$list_urlval = array();
for ($i = 0; $i < $hrefs->length; $i++) {
        $nValue = $hrefs->item($i);   
        $href = $nValue->getAttribute('href');
        $value = $nValue->nodeValue;
       
        if($href != '' && (!preg_match("/#/", $href)) && $href != '/' && (!preg_match("/javascript/", $href)) && (!preg_match("/mailto/", $href)) && (!preg_match("/plus.google/", $href))){
       
            if((!preg_match("/http/", $href)))
                $href = $urlname.'/'.$href;
               
            $list_urls[] = $href;
            $list_urlval[] = $value;
        }
}

print_r(array_unique($list_urls));


Friday, December 21, 2012

Find All Links on Web Page

$html = file_get_contents('http://www.TestDomain.com');

$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

$list_urls = array();
for ($i = 0; $i < $hrefs->length; $i++) {
       $href = $hrefs->item($i);    
       $list_urls[] = $href->getAttribute('href');
       //echo $url.'<br />';
}
echo '<pre>';
print_r(array_unique($list_urls));
echo '<pre>';

Saturday, December 15, 2012

Increase media library upload file size limit


Add this code in htaccess:

php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200

You can increase the file upload limit according to your requirement.

 OR

Save this code in any php file and update it in the plugins directory and activate that plugin:

ini_set('upload_max_size','256M');
ini_set('post_max_size','256M');
ini_set('max_Execution_time','600');

OR

So here are the proper steps:

1) Use FTP to download a copy of your php.ini file
2) modify that file to have the following lines:
memory_limit = 100M
upload_max_filesize = 192M
post_max_size = 100M
file_uploads = On

Tuesday, December 11, 2012

MSVCR100.dll missing when wampserver install - [resolved]


For Windows 32 : Be sure that you have installed the Visual C++ 2010 SP1 Redistributable Package x86
: VC10 SP1 vcredist_x86.exe >>
http://www.microsoft.com/download/en/details.aspx?id=8328


For Windows 64 : Be sure that you have installed the Visual C++ 2010 SP1 Redistributable Package x64
: VC10 SP1 vcredist_x64.exe >>
http://www.microsoft.com/download/en/details.aspx?id=13523

Error HTTP 403 - forbidden in wampserver-[resolved]


Putting server online in contect menu did not help me. If you are using wampserver you will find the lines in your httpd.conf file

#   onlineoffline tag - don't remove
Require local
 
Change it to

#   onlineoffline tag - don't remove
Require all granted
 

Friday, December 7, 2012

PHP resize an image without losing quality - [resolved]

PHP resize an image without losing quality, we will explain how we can resize an image through PHP, with only 3 lines of code. And most importantly, without the result is a pixelated image.

We begin:

To do this we will use the Imagick PHP Library (Image magic), which is included in PHP itself from version 5.1.3. As a second step we must ensure that the hosting site where we stored the library have this enabled, some do not bring it and this causes many errors. If we are running with WAMP or XAMP the web, it would not hurt to look for a tutorial on how to activate services and libraries for PHP in these local systems.

* A requirement before proceeding with the process is that the image should be resized before being uploaded to our server before.

We start by instantiating the class constructor, which receives as parameter the full path to the image hosted on our server (including extension):

$image = new Imagick('Imagename');

In the variable $image keep our Imagick object for treatment, after this, simply call the method cropThumbnailImage, whose parameters are width and height (in order):

$image->cropThumbnailImage(width[type int],high[type int]); //this function is used for croping image

OR

$image->resizeImage('500','691', imagick::FILTER_LANCZOS, 0.3); // this function is used for imageResize


As a last step we can only save the image you just cut, for it has the method Imagick writeImage whose expected parameter is the path where you want to save the image but the name of this. (please add the suffix _thumb the name of the image):

$image->writeImage( 'target_folder' );

This done, get our cropped image with PHP, without loss of quality, and without the result is a pixelated Thumbnail

For more information about this library, you can visit the PHP API: Image Magic PHP

Thursday, December 6, 2012

How to show a hierarchical terms list?

<ul>
    <?php $hiterms = get_terms("prod_cat", array("orderby" => "count", "parent" => 0, "hide_empty" => 0)); ?>
    <?php foreach($hiterms as $key => $hiterm) : ?>
        <li>
            <?php echo $hiterm->name; ?>
            <?php $loterms = get_terms("prod_cat", array("orderby" => "slug", "parent" => $hiterm->term_id, "hide_empty" => 0)); ?>
            <?php if($loterms) : ?>
                <ul>
                    <?php foreach($loterms as $key => $loterm) : ?>
                        <li> - <?php echo $loterm->name; ?></li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>