Category : "Coding"

All kinds of Programming.

Check if php is running from cli (command line)

Php from command line PHP scripts are usually opened in a web browser. But they can be run from command line or terminal as well. The syntax to run a script from commandline is very much similar to python or perl. php /path/to/script/index.php Inside a script it might be necessary to test if it is being run from command line or not. For ...

Set mysql timezone at runtime via PHP

To set the timezone of mysql at runtime via php use the following code : $c = mysqli_connect($host, $username, $password); mysqli_query($c , "SET `time_zone` = '".date('P')."'"); This can be executed when the connection is established and it will set the timezone of mysql to whatever timezone php is set to. Fast and effective. ...

PHP download remote file via url

The following function will download and save a remote file , using its http url : function download_remote($url , $save_path) { $f = fopen( $save_path , 'w+'); $handle = fopen($url , "rb"); while (!feof($handle)) { $contents = fread($handle, 8192); fwrite($f , $contents); } fclose($handle); fclose($f); } The method reads 8192 bytes at a time , and not the whole file , since if the whole file is read at once , then ...

Make a racing car using Box2d in javascript

Racing car in box2d Box2d is a physics 2d simulation library and is used in game development. Apart from C/C++ it has ports for various languages like python, javascript etc. With the availability of the html5 canvas element and many other html5 apis, it becomes possible to write browser based games in javascript. So in this article we shall try to ...

Resize large images in php with imagemagick

Resizing images Web applications often need to work with images like pictures of users or photos etc. So the need to need to resize images comes up when creating thumbnails for example or compressing images for storage. The default library to handle image operations in php is GD. It can be used to resize images. Resize with GD The function imagecopyresampled is used ...

htaccess php_flag not allowed here

If using this code <IfModule mod_php5.c> #Turn off php register globals php_flag register_globals off </IfModule> gives the error : .htaccess: php_flag not allowed here in /var/log/apache/error.log Then Options should be added to AllowOverride in : /etc/apache2/sites-available/default AllowOverride FileInfo Options like this <Directory /var/www/> Options Indexes FollowSymLinks MultiViews ...

PHP Urlencode equivalent in Ruby

If you are finding a way to encode urls in ruby then here is a quick tip - Short Answer: You could use the URI class or the CGI class. We have CGI.escape and URI.escape, but they have some slight differences. I have tried to show the differences in IRB as follows - $ irb irb(main):001:0> str = 'hello world' => "hello world" irb(main):002:0> require 'uri' => ...

Find where a method is defined in Ruby

There are times when you might be working on a codebase with other people and spot a method which you do not know about. You may want to locate the source of that method to learn and understand more about its functionality. So, Here is a quick tip on how to find where a method is defined or located. I was working ...

Check system load in php on linux

System load System load is a measure of how busy the system is running the processes. Higher system load simply means more processes are running and more are waiting to be run. When coding php applications, it sometimes is useful or necessary to find the current load on the server/system. For example if you want to run a long resource intensive ...

List foreign keys in mysql

information_schema The following query will list out the foreign keys in mysql. It finds it out from the information_schema database. select concat(table_name, '.', column_name) as 'foreign key', concat(referenced_table_name, '.', referenced_column_name) as 'references' from information_schema.key_column_usage where referenced_table_name is not null; The output is a clean table listing out all foreign keys ...



Connect with us