المشاركات

عرض الرسائل ذات التصنيف php

How to Install Laravel 5.6 on Windows using Composer

صورة
Here let's see how to install laravel on windows using composer . Ever since its launch, laravel has become the most popular php framework and not without a good reason. It's a great alternative to CodeIgniter which is another popular MVC for php. Laravel ships with so many goodies out of the box than other frameworks do. Mastering laravel will definitely take some time, but the time spent is well worth it in the long run. And it will save loads of time in application development. Anyhow, here you are, planned to move on to laravel and want to install it for the first time. Without wasting the time, let's dive into the process of installing laravel on windows machine. System Requirements for Laravel: As of now, the latest laravel version is 5.6. And you have to make sure the below system requirements is met to install and use it on your machine. PHP >= 7.1.3 OpenSSL PHP Extension PDO PHP Extension JSON PHP Extension XML PHP Extension Ctype PHP Extension Mbst...

How to Create and Download CSV File in PHP

صورة
Hi! Here we will see how to create a csv file and download it using php . CSV is one of the popular data storage methods used on the Web. Being a modern language, PHP has no problems handling various data formats including csv. It offers native functions to read and write csv files. With fputcsv() method, you can write data as a csv file and force it to download. Clicking on a file url (link) will just open it in the browser window without downloading. The exe and zip formats are an exception here. But in case you need to download it directly to client's hard disk, then you have to make use of the readfile() function. Let's see how to do it. PHP - Create CSV File: The following snippet creates a csv file named 'myfile.csv' on your current working directory. <?php // data array $user = array(1, 'Johnson', 'johnson@mydomain.com', 'Miami'); // filename $filename = 'myfile.csv'; // write to csv file $fp = fopen($filename, 'w...

How to use PHP PREG MATCH function to validate Form Input

صورة
In this post we'll see how to do some basic PHP validations of form input using preg_match() function. One of the vulnerable spot in a website which attracts malicious hackers are the user input forms like registration form, contact form etc. Validating the user input before processing is the first and foremost step in securing the site. The validation includes checking if the data we received is in the right format and length. Generally it's a practice among web developers to do validation check at the client side (like java script). Still it’s easy for someone to break thru it and harm your site. So it's strictly advisable to do these validations on the server side (like PHP) . Generally we receive the form input as string and we can use preg_match with appropriate regular expression to check a required pattern in the input string. Preg Match Syntax Before dwelling into the validation process, here take a sneak peak at the syntax of preg match function. ...

How to Secure Passwords in PHP and Store in Database

صورة
Hi! Here let's see how to secure passwords in php . Developers have a huge responsibility when handling sensitive user data, such as password. They must take necessary precautions to store the password and other sensitive information in a secure manner. Old-school methods use the md5 algorithm to hash passwords and store them in the database. This really is not safe and vulnerable to attack. But thanks to the community, PHP 5.5 and higher comes with the password_hash function that generates one-way hash that is extremely secure to store passwords in the database. Below, we will see how to securely hash passwords, store them in the database and verify them against the user given password in php. PHP - Secure Way to Store Passwords: If you are a budding developer, these are some things to keep in mind when handling the password. Never store passwords as plain text. It's as good as not having a password at all. Never use MD5 or SHA1 for hashing. They are extremely fast an...

How to Convert PDF to JPEG Image in PHP

صورة
Hi! Today let's see how to convert pdf to jpeg in php using imagick . PHP offers some good native extensions for image processing. Imagick is one of those extensions with which we can easily create JPEG images from pdf without the need for third-party tools. The library does not need installation since it comes built-in with PHP. You just have to instantiate the class and use it. Plus it provides several customization options to create images. I suggest you to refer the official documentation for the complete list of functions. PHP - Convert PDF File to JPEG: Imagick is the native php extension to create and process images using the ImageMagick API . To covert the pdf into jpeg with imagick, you must first read the image from the pdf file and then write it into an image. The following example converts all the pages in a pdf file into jpeg images. <?php $imagick = new Imagick(); $imagick->readImage('mypdf.pdf'); $imagick->writeImages('myimage.jpg', fa...

Send HTML Email in PHP via SMTP

صورة
Hi! Welcome to Koding Made Simple . Today we'll see how to send html email in php . PHP's mail() function is simple and effective and let you send emails with text/html contents and with attachments. Though it has some shortcomings compared to other mailer libraries like PHPMailer , it does the job and comes in-built with PHP package. Plain text emails are good enough, but the ability to send html emails is more powerful in email marketing and other promotions. HTML emails include html tags and let you add images, format texts and other eye-catching call-to-action buttons etc. But you have to add additional headers for mailing them. Using SMTP for Sending Email You can send mails via third party Mail Servers but you need authentication first. That is if you want to send email via Gmail in php, then you must have a working gmail account and provide the accounts email-id and password for authentication. We'll see how to set up the php configuration for sending ema...

Dynamic Treeview Menu using PHP, MySQL and AJAX Example

صورة
How to Create Dynamic Treeview Menu using PHP, MySQL and AJAX? Most modern websites use tree view to display the dynamic sidebar menu for easy navigation. In case you don't know, a Treeview is a hierarchical representation of elements in a tree-like structure. You can go for jquery solution in this context, but I would recommend 'Bootstrap Treeview' plug-in if you use the bootstrap framework to build your websites. The plug-in uses JSON dataset to create a hierarchical tree structure. I have already discussed the creation of static treeview menu using bootstrap treeview . But you can also generate a dynamic tree where you pull off the data elements stored in the database. In this tutorial I'm going to show you about creating dynamic treeview using php, mysql, ajax and bootstrap. How to Create Dymanic Treeview in PHP & MySQL? For the demo, I'm going to load all the required libraries via CDN. So there's no need to download them to your web server. Here...

Convert HTML to PDF in PHP using DomPDF Library

صورة
Hi! In today's post we will see how to convert html to pdf in php using dompdf library. DomPDF is basically a php library that offers a simple way to convert html content to pdf so that you can generate pdf files on fly with php. Frameworks like Laravel offers separate packages to create pdf files, but there is not so much luck when you work on core php. Hence we need external tools for the task and DomPDF is a good fit for it. The library creates downloadable pdf file from the html webpage. It supports CSS2.1 and CSS3 and renders the html layout including the styles. It also handles @import, @media and @screen queries and can load external stylesheets while generating the pdf. 1. Convert HTML to PDF - Basic Usage: Download the dompdf archive from here and extract the contents to your root folder. After that, create a sample php file, 'index.php' and include the autoloader file to load the required dompdf libraries and helper functions into your PHP project. The fol...

PHP - Send Email via SMTP Server using PHPMailer

صورة
Hi! In this post, let's see how to use PHPMailer to send email from PHP . PHPMailer is an amazing php library to send emails quickly. We all know the generic PHP's mail() function helps you send emails. But on the downside, it requires local mail server to send them. And also the setup and configuration is not that easy. But that's not the case with PHPMailer. The library supports SMTP protocol and allows easy authentication over SSL and TSL. It allows you to display error messages in more than 40 languages in case there is an error when sending emails. PHPMailer uses the real valid email address to send mails. Hence you have to setup one if you don't have it. Here, let's see how to use Gmail SMTP to send email via PHPMailer. Sending Email via Gmail SMTP using PHPMailer: SMTP is basically a mail protocol that sends a request to the mail server and after verification sends it to the mail server of the recipient. To send an email via smtp, you need the authenti...

How to Display Random Image from Folder in PHP

صورة
Hi! This is a quick tutorial about displaying random image from folder in php . It will be useful if you want to include featured section area on websites. Usually site owners will showcase their products or services in the prominent section with attractive pictures. And all these images will come from a folder and rotated through at regular time interval. A random image will be picked up from the lot one at a time and displayed here. Let's see how to implement this random image selection process. PHP offers a rich set of native functions to handle directory and files. And they are more than enough for the task we are going to do. PHP - Displaying Random Image from Folder: As for the process goes, we have to use scandir() function to read all the files from a given folder and make a random pick in the lot. Just follow the below steps to choose a random image file in a directory and display it on the browser. Step-1) First define the path to the image folder. $dir_path = ...

How to Download File from URL using PHP cURL

صورة
Hi! In this post, we will see how to download file from url using php curl . CURL is a great tool when it comes to remote communication. Using it, you can easily connect to a remote server and download files to your local machine. It allows to send http post request and get request in php as well. Executing a basic curl request will simply return the data to the output stream. But we don't want that. Instead, we must assign it to a php variable, which we can write it to the disk. Using CURLOPT_RETURNTRANSFER is the easiest way to copy remote files to our own. But there is a problem with this method. Below we will see the right way to download remote files with curl. PHP - Download File from URL using cURL: To download files from the remote url with curl, you have to follow the below steps: Create a writable file stream Pass the file handle to curl This will make cURL write the content downloaded directly into the file. Close the file handle. As I said earlier, using C...

How to Post JSON Data with PHP cURL & Receive It?

صورة
Hi! Here let's see how to post json data with php curl and receive the raw json sent . You can simply use PHP file_get_contents() to send HTTP GET request . But it's not the same case with HTTP POST. Fortunately PHP cURL makes the process very simple. With curl you can easily send HTTP requests. In case you don't know, 'cURL' is a library that let you communicate with web servers using different protocols such as http, https, ftp, telnet, file etc. To use curl, you must install libcurl package on your server according to the version of PHP you use. In case you are not sure if curl is enabled on your machine or not, use this code to check it. <?php echo (is_callable('curl_init') ? 'cURL is enabled' : 'cURL is disabled'); ?> How to Post JSON with PHP cURL? In order to send HTTP POST request with JSON data, you have to follow some steps and they are, Format JSON data correctly Attach JSON to the body of the POST request Set th...

Paytm Payment Gateway Integration in PHP - Example

صورة
Hi! In this tutorial, we will see how to integrate paytm payment gateway in php . Mobile wallets have become immensely popular in recent years and paytm is highly trusted among online customers. Many people use it every day for doing online recharges, DTH & other bill payments. It reduces the risk of exposing customer's banking credentials and credit card details to hackers. Therefore, Paytm is a good solution for online payments on your Website and Mobile Apps. Integrating the paytm gateway in your application is quite easy and below I'll show how to do it with php. Paytm Payment Gateway PHP Integration: If your application is based on php, then you don't have to go from scratch. You can quickly integrate paytm into the application using the paytm kit for php . It covers all the basics, you just have to download and use it in the following way. Step-1) Download Paytm Kit Download Paytm Payment Kit for PHP and extract its contents. Then move the 'Payt...

CodeIgniter | Remove index.php from URL using htaccess

صورة
Hi, we’ll see how to remove index.php from url in codeigniter . Even though codeigniter uses search engine friendly urls, nobody would have missed the awkward presence of 'index.php' in between those codeigniter's urls. Have you ever wondered how to get rid of it to get more clean urls in your codeigniter application ? Say your app has an url something like http://www.example.com/index.php/contact and you want it to look like http://www.example.com/contact . Yes, you can do it. In codeigniter removing index.php from url should be done by rewriting urls in htaccess file. How to Remove index.php from URL in CodeIgniter using .htaccess? Recommended Read: How to Create Login Form in CodeIgniter, MySQL and Twitter Bootstrap Recommended Read: CodeIgniter/MySQL Tutorial - Read and Display data with Twitter Bootstrap Step 1: Enable Mod Rewrite Option in your APACHE Server To rewrite urls in the htaccess file, the mod_rewrite option should be enabled in the apache serve...

How to Merge Two JSON Strings into One with PHP

صورة
Hi! In this tutorial we will see how to merge two json strings into one in php . People often mistakes JSON for object or array but it's the string representation of a JavaScript object. In a recent project, I wanted to combine two jsons. That is, I have to make two API calls and combine their results into one. It is a rest api and returns the response as json. The task is simpler than I thought. It employs a similar technique I used here earlier when dealing with json. Let me show you how to do it. PHP - Merging Two JSON into One: If you are new to working with JSON, don't worry. PHP offers native functions to handle it easily. The idea is to decode the json data, write it to an array and encode it back to json. Below are the steps involved in the process. STEP-1) Let's take two different jsons containing couple of employee details. <?php $json1 = '{"id": "GD01", "name": "Garrett Davidson", "position...

How to Get Country Name from IP Address with PHP

صورة
Hi! In this post, let's see how to get country name from ip address using php . When working on multilingual web applications, it is mandatory to find the geographic location of the visitors, so that you can provide the contents that match the specific location. Some websites even change country flags and themes accordingly. With PHP, you can get the visitor's IP easily, but unfortunately there's no way to get the country details from it. Hence, you have to rely on external web services to provide the location details. Here I'm going to show you how to find the country of visitors with the help of GeoPlugin API and PHP. Getting Country Name from IP Address with GeoPlugin: GeoPlugin is a free web service that provides geographical location of site visitors from IP. It offers various types of services like PHP, JSON etc., and in this tutorial, I'm going to use the JSON API to find the country name . You have to access the service with this url, http://ww...

Get JSON from URL in PHP

صورة
In this tutorial, I’m going to show you how to get json from url in php script . JSON has become a popular way to exchange data and web services outputs in json format. To send a HTTP request and parse JSON response from URL is fairly simple in php but newbies may find how to parse json difficult. Let's see how to build a php json parser script. For this script, I’m going to access Google MAP web service via API and get latitude and longitude co-ordinates for a location. Google map api produces both json/xml output. But for this example I’m going to get the json response and show you how to parse json object to retrieve the geo-metric details. Read more »

How to Calculate Age from Date of Birth using PHP

صورة
Hi! Here we will see how to calculate age from date of birth using php . Working with dates is essential in web development and what we are going to see is a small but very useful utility. Applications such as membership driven websites, social platforms, etc., show the age of the users on their profile page, which they calculate from the date-of-birth of the users. Finding age with the birth date is very easy in php. If you use PHP 5.3+, consider yourself fortunate as it offers a built-in function called date_diff() that simplifies the task. Using Date_diff() Function: The function date_diff() in php takes two DateTime objects and returns the difference between them. You can't pass a date string as it is to the function. First you must convert it into a DateTime object using date_create() and then pass them as arguments. Consider the following example, <?php $date1 = date_create('2017-7-10'); $date2 = date_create('2017-7-17'); $interval = date_dif...

Get Zipcode From Address using PHP and Google Maps API

صورة
Hi! We were seeing about Google Maps API for a while, and today's post is also one on the line. In this tutorial, we are going to see how to get zipcode from address using php and google maps api . Sometimes you might have a large set of customers addresses without postal codes. In such cases you can rely on Google maps to provide the pin code you need. It serves queries related to addresses with less effort. So with the help of Google Maps Geocoding API you can easily extract the zip code from the address and I'm going to show you how to do it in php. PHP - Get Zipcode from Address using Google Maps API: Retrieving zipcode/pincode from the address is a two step process. First we have to get the latitude and longitude for the given address and then use them to get the zip code details. The API offers two different types of services, geocoding and reverse geocoding of addresses and we have to use both to achieve the desired result. This is the web service url we have...

How to Filter Multidimensional Array by Key Value in PHP

صورة
Hi! In today's post, let's see how to filter multidimensional array by key value in php . PHP offers extensive functions to manipulate arrays of which array_filter makes the unimaginable possible. The function allows you to filter elements of an array with custom callbacks. Multi-dimensional arrays are complicated enough but filtering them by key value is a headache because you have to iterate over each element and look for the specific key values to filter it. But PHP's array_filter() function provides a short and simple way to filter multidimensional array by key and value. You have to use the appropriate callback filter and the rest is cake walk. PHP - Filtering Multidimensional Array by Key Value: Before we start the process, a little intro about the function array_filter() . It let you to filter array by value using custom callback. It takes up three parameters, 1. the source, 2. callback function that acts as the conditional filter and 3. flag to define w...