Sometimes you want to get the visitor’s IP address (e.g to show it to him or for logging purposes). This simple snippet does the job: $ip = $_SERVER[‘REMOTE_ADDR’]; Also, if you have REGISTER_GLOBALS turned on on your server, you can use the shorter version: $ip = @$REMOTE_ADDR; Then you can easily use the variable called […]
Category: Backend Development
Top Online IDEs to Test Your PHP Code
Sometimes you don’t have an Apache server on your PC and you don’t want to go through the hassle of uploading your PHP script to your web hosting space just to test if your tweaks inside the code work. Sometimes you just need to paste the code inside the IDE (Integrated Development Environment) and see […]
Convert Text to Barcode Image
Thanks to a skilled PHP programmer, the internet has a ready made PHP script that takes a text string and returns an image representation in barcode format. Here’s the complete script: =1 to enable NOTE: You must have GD-1.8 or higher compiled into PHP in order to use PNG and JPEG. GIF images only work […]
How to kill PHP (on purpose)
1. Stack overflow (e.g calling a function inside itself). function a() { a(); } a(); First you define a function called a() and the function’s sole purpose is to call itself again, causing an infinite loop. Then you just call the function. 2. Excessive Memory Allocation Using str_repeat str_repeat(“a”, 10000000000); The above code will cause […]
Easy PHP Debugging
Spotting errors inside PHP is a difficult task. Especially if you try to understand the default messages, given by the PHP interpreter. If you want to effectively debug your PHP script, you can use the following code: error_reporting(E_ALL); This code will show all errors, notices and warnings generated during the script’s run. It’s very useful […]
Connect to a MySQL Database
This is the very basic step, needed to work with MySQL databases. Before trying to do any actions on the database itself, such as: creating tables, querying rows, dropping tables or else, you have to make your PHP script connect to the MySQL database. $host = “mysql_host”; $user = “mysql_user”; $pass = “mysql_password”; $db = […]