Skip to main content

Posts

Create Thumbnail

To generate thumbnail, here is the  simple and easiest way. It will help you in creating thumb for jpg, png and gif images. /* generateThumb() * * @param mixed $source Path to source file * @param mixed $destination Path to destination file * @param mixed $width Thumbnail file width * @param mixed $height Thumbnail file height * @return bool */ function generateThumb($source, $destination, $width = 100, $height = 100){ $ext = strtolower(substr($source, strrpos($source, ".")+1)); $format = ($ext == 'jpg')?'jpeg':$ext; $from_format = "imagecreatefrom".$format; $source_image = $from_format ( $source ); $source_width = imagesx ( $source_image ); $source_height = imagesy ( $source_image ); $ratio1 = $source_width/ $width; $ratio2 = $source_height / $height; if($ratio1 > $ratio2){ $width = $width; $height = $source_height/$ratio1; }else{ $width = $source_width/$ratio2; $height = $height; } $target_image = imagecr...

Compare 2 mysql databases

Below code will list all the tables with fields (& type) and total records in each table, so that we can verify whether fields are same in both database fields and record counts. <?php mysql_connect("localhost", "user1", "pwd1") or die(mysql_error()); mysql_select_db("db1") or die(mysql_error()); $tables = array('tbl1', 'tbl2','tbl3'); echo "<table  border='1' style='float: left'>"; foreach($tables as $tbl){   $sel = "SELECT COUNT(1) FROM $tbl";   $res = mysql_query($sel);   $rec = mysql_fetch_row($res);     echo "<tr><th colspan='2'>$tbl ($rec[0])</th></tr>";   $sel1 = "SHOW FIELDS FROM $tbl";   $res1 = mysql_query($sel1) or die($sel1.mysql_error());   while($rec1 = mysql_fetch_row($res1)){     echo "<tr><td>$rec1[0]</td><td>$rec1[1]</td></tr>";   } } ech...

HTTP POST without cURL using PHP

I don't think we do a very good job of evangelizing some of the nice things that the PHP streams layer does in the PHP manual, or even in general. At least, every time I search for the code snippet that allows you to do an HTTP POST request, I don't find it in the manual and resort to reading the source. (You can find it if you search for "HTTP wrapper" in the online documentation, but that's not really what you think you're searching for when you're looking).  So, here's an example of how to send a POST request with straight up PHP, no cURL: <?php      function do_post_request($url, $data, $optional_headers = null) {           $params = array('http' => array(                                 'method' => 'POST',                   ...

SQL JOINS

Practice and crack the interviews on JOINS.

4 Most Important PHP Security Measures

We can say that PHP is a mature language with lot's of useful, but potentially dangerous features. The rapid growth of the language and the dynamic nature of the Web let people easily create dynamic web pages without any prior knowledge in computer science or the architecture of the Internet. In this tutorial we’ll have a look at 4 important PHP security measures that you should implement in order to develop a safer website. 1. Register Globals Up until PHP version 4.2.0 the register_globals directive's default value was On . One of the most controversial change in following versions was that the PHP core developers changed this default value to Off , not because the directive itself was insecure, but the common misuse of it was. Note: This feature will be removed starting with PHP 6.0.0 When this directive is On , PHP will inject extra variables in the script such as HTML request variables, etc. The problem with this approach is that a developer canno...

Security: Password Hashing

In this article I'm going to cover password hashing, a subject which is often poorly understood by newer developers. Recently I've been asked to look at several web applications which all had the same security issue - user profiles stored in a database with plain text passwords. Password hashing is a way of encrypting a password before it's stored so that if your database gets into the wrong hands, the damage is limited. Hashing is nothing new - it's been in use in Unix system password files since long before my time, and quite probably in other systems long before that. In this article I'll explain what a hash is, why you want to use them instead of storing real passwords in your applications, and give you some examples of how to implement password hashing in PHP and MySQL. Foreword As you read on you'll see that I advocate the use of a hashing algorithm called Secure Hashing Algorithm 1 (or SHA-1). Since I wrote this article, a team of researcher...

MySQL 4.1+ using old authentication

When I was working with XAMPP in Ubuntu and asked write PHP script to connect to remote MySQL server which is using PASSWORD hash function to save the password for user, and I found following error. Warning: mysql_connect() [function.mysql-connect]: Premature end of data (mysqlnd_wireprotocol.c:554) in path/to/the/file/where/connection/script/is/written/ Warning: mysql_connect() [function.mysql-connect]: OK packet 1 bytes shorter than expected in path/to/the/file/where/connection/script/is/written/ Warning: mysql_connect() [function.mysql-connect]: mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password'). This will store a new, and more secure, hash value in mysql.user. If this user is used in other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag from your my.cnf file in path/to/the/file/where/conn...