PHP cURL: Fetching URL and Sending Request with Cookies

One of the things the remote web server inspects is the client cookie to know about the requester. If you need cURL to simulate a user browser that sends cookie information to the web server, you need the following options: $c = curl_init(‘http://www.example.com/needs-cookies.php’); curl_setopt ($c, CURLOPT_COOKIE, ‘user=ellen; activity=swimming’); curl_setopt ($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec …

PHP cURL: Fetching URL and Sending Request with Cookies Read More »

PHP cURL: Making POST Request to URL

By default, all HTTP requests made out by cURL is GET requests that simply fetches the URL and don’t submit any more POST variables. However, if you need to fetch and retrieve URL by the POST method with cURL, you need the snippet below: $url = ‘http://www.example.com/submit.php’; // The submitted form data, encoded as query-string-style …

PHP cURL: Making POST Request to URL Read More »

PHP: GD Library Drawing Functions Reference

To draw a line, use ImageLine( ): ImageLine($image, $x1, $y1, $x2, $y2, $color); To draw an open rectangle, use ImageRectangle( ): ImageRectangle($image, $x1, $y1, $x2, $y2, $color); To draw a solid rectangle, use ImageFilledRectangle( ): ImageFilledRectangle($image, $x1, $y1, $x2, $y2, $color); To draw an open polygon, use ImagePolygon( ): $points = array($x1, $y1, $x2, $y2, …

PHP: GD Library Drawing Functions Reference Read More »

PHP: Change Error Reporting Level | Different PHP Error Types

Debugging a PHP script, you may want to alter the error-displaying sensitivity on a particular page and control what types of errors should be reported. The solution is the PHP error reporting function error_reporting(): error_reporting(E_ALL); // everything error_reporting(E_ERROR | E_PARSE); // only major problems error_reporting(E_ALL & ~E_NOTICE); // everything but notices As the parameters of …

PHP: Change Error Reporting Level | Different PHP Error Types Read More »

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File

Of course, you will have to read in the file first, preferably in an array. To read a file in an array, you just need the file() function: $lines = file(‘quotes.txt’); Then, you shuffle the array with shuffle() function that randomizes all the items in the array thus shuffling the lines in the file quotes.txt: …

PHP: Randomizing All Lines of a File – Shuffle Lines in a Text File Read More »

PHP: Count Lines of a File and Get the Number of Lines in a Text File

As we know, lines are separated from each other by line breaks indicated by “\n” in PHP, therefore, one method would be to explode() the entire file content string by “\n” into an array and then count() the number of the array items to come to the number of lines of that file. However, there’s …

PHP: Count Lines of a File and Get the Number of Lines in a Text File Read More »

PHP: Reading a File into a String or Reading the File into an Array

Aside from the common approach in reading a file with php fopen() function which is a little annoying in that it requires extra steps before actually reading the contents of the file. PHP has other file reading functions to enable you instantly get what you want and read the contents of the file without the …

PHP: Reading a File into a String or Reading the File into an Array Read More »

PHP: Make or Create A Directory

The function in PHP that’s used to create a directory is just the same with that in Linux bash shell: mkdir. First make sure the current working directory is where you want the new directory to be, then run this line of PHP: mkdir(‘new_dir’); // creates a new directory named new_dir under the current directory

PHP: Delete File / Erase File / Remove File

As long as the owner of the php script has the necessary privilege to delete a file, the php script has the ability to delete it. To delete a file with PHP, use the unlink() function: unlink(‘tmpfile.txt’) or die(‘Unable to delete the file’); It returns true if the deletion is successful and false otherwise.

PHP: Getting Directory Path and Filename from A Full Path or URL

It’s common tasks to parse and deal with file paths or URL in PHP. And most common must be to identify and distinguish the directory path or file name from the file path or URL. For instance, the full path to a text file is: ‘/home/john/lanning.com/somefile.txt‘ and you want get the directory part as well …

PHP: Getting Directory Path and Filename from A Full Path or URL Read More »

Linux: The differences between file times: atime (accessed time), ctime (changed time) and mtime (modified time).

Unless you have explicitly opted out with a noatime option mounting your Linux file system, there are generally 3 types of time on each and every file of Linux: atime or access time, ctime or change time, and mtime or modification time. These are the differences between the 3 file system times: atime — access …

Linux: The differences between file times: atime (accessed time), ctime (changed time) and mtime (modified time). Read More »

Scroll to Top