All posts by mahesh

PHP: Simple function to encode and decode string in php

Sometimes you need to store the data in database in encrypted format.

 

No worries, use these below very simple function to encode and decode string in php.
Below are the functions for doing same and is very easy to use, you can customize these as per your requirement to make it very strong encoded.

 

PHP Function for encrypt a string

<?php
    /**
    * Function to encrypt the string
    * will provide you a very strong encoded string
    */

    function encode5t($str)
    {
		for($i=0; $i<5;$i++){
			//apply base64 first and then reverse the string
			$str=strrev(base64_encode($str));
		}
			return $str;
    }
?>

Function to decrypt the encrypted string

<?php
    /**
    * Function to decrypt the string (that was previously encoded)
    */

    function decode5t($str)
    {
		for($i=0; $i<5;$i++){
			//apply reverse the string first and then base64
			$str=base64_decode(strrev($str));
		}
			return $str;
    }
?>

Marquee tag in HTML and its attributes

The HTML marquee tag provides a way for browsers to creates a scrolling effect (can be tailored to include any HTML content) vertically or horizontally without having to resort to JavaScript techniques.

 

 

The marquee is non-standard HTML tag but you can enjoy (or possibly suffers from) good browser support.

 

You can change the marquee attributes on MARQUEE tag by following ways.

 

Attributes:

 

BGCOLOR=”#CCCCCC” – Set background color.

LOOP – Determines how many times the text will scroll. -1 is indefinite and will continuously scroll. You can set this to whatever you’d like.

SCROLLAMOUNT – Determines the speed your text will scroll.

WIDTH – Determines the width of your marquee.

HEIGHT – Determines the height of your marquee.

Direction – Determines the direction in which the text should scroll – up or down.

BEHAVIOR=”slide” – Defines the type of scrolling.

SCROLLDELAY=”500″ – Defines how long to delay between each jump.

HSPACE – Specifies horizontal space around the marquee.

VSPACE – Specifies vertical space around the marquee.

 

<marquee
bgcolor="color name or hex value"
loop="value (number of loops)"
align="{ top | middle | bottom }"
direction="{ left | right }"
behavior="{ scroll | slide | alternate }"
height="integer (pixels or %)"
width="number (pixels or %)"
hspace="integer (pixels or %)"
vspace="number (pixels or %)"
scrollamount="number"
scrolldelay="number">
</marquee>

 

Event Attributes:

onclick
Script runs when a mouse click

ondblclick
Script runs when a mouse double-click

onmousedown
Script runs when mouse button is pressed

onmouseup
Script runs when mouse button is released

onmouseover
Script runs when mouse pointer moves over an element

onmousemove
Script runs when mouse pointer moves

onmouseout
Script runs when mouse pointer moves out of an element

onkeypress
Script runs when key is pressed and released

onkeydown
Script runs when key is pressed

onkeyup
Script runs when key is released

 

Check whether a PHP extension is loaded or not

How to figure out whether PHP extension is loaded or not on your page?

 

Usually in phpinfo() doesn’t show you all the loaded extensions in one location, it has got a separate section for each loaded extension where it shows all of its variables, file paths, etc so if there is no section for your extension name it probably means it isn’t loaded.

 

You can also check by below PHP function, if you know the exact name of PHP extension.

 

<?php
//gd represented GD Library as you usual say
if (!extension_loaded('gd')) {
	if (!dl('gd.so')) {
		exit;
	}
}
?>

Returns Boolean value true if the extension loaded and false otherwise .

Some other PHP extension related functions are

 

get_loaded_extensions()
Returns an array with the names of all modules compiled and loaded

 

get_extension_funcs()
Returns an array with the names of the functions of a module

 

dl()
Loads a PHP extension at runtime

 

Note: dl() is disables in some environment and it will also disabled in PHP Safe Mode.

 

A simple PHP script for MySQL Database backup

PHP: Backup Your Website’s MySQL Database

 

It is always good to backup your MySQL database regularly. You should also create a backup before making any major changes, in case something goes wrong and you need to revert to the unmodified version, then you will found yourself in safe hand. Database backups can also be used to move your database from one server to another if you change hosting company.

 

If you want to take backup of your complete database using the command prompt then follow the below command.

 

mysqldump -u yourusername -p yourpassword yourdatabasename > yourbackupfilename.sql

 

Please replace yourusername, yourpassword, yourdatabasename, yourbackupfilename according to your real credentials.

 

You can also do the same job using ‘php script for mysql database backup’.
Follow the below script to have backup of your complete database or some tables.

 

You just need to provide database credentials to the function, as well as an array of tables you’d like to backup leave it if you want to take complete database backup.

 

<?php
    backup_tables('HOSTNAME','USERNAME','PASSWORD','DATABASE');

    /* Function to create backup of a database */
    function backup_tables($host,$user,$pass,$name,$tables = '*')
    {
		$link = mysql_connect($host,$user,$pass);
		mysql_select_db($name,$link);
	
		if($tables == '*'){
			$tables = array();
			$result = mysql_query('SHOW TABLES');
			while($row = mysql_fetch_row($result)){
				$tables[] = $row[0];
			}
		}else{
			$tables = is_array($tables) ? $tables : explode(',',$tables);
		}
	
		//Looping of tables
		foreach($tables as $table){
			$result = mysql_query('SELECT * FROM '.$table);
			$num_fields = mysql_num_fields($result);
		
			$return.= 'DROP TABLE  IF EXISTS '.$table.';';
			$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
			$return.= "\n\n".$row2[1].";\n\n";
		
			for ($i = 0; $i < $num_fields; $i++)
			{
				while($row = mysql_fetch_row($result))
				{
					$return.= 'INSERT INTO '.$table.' VALUES(';
					for($j=0; $j<$num_fields; $j++)
					{
						$row[$j] = addslashes($row[$j]);
						$row[$j] = preg_replace("/\r\n/","\\r\\n",$row[$j]);
						if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
						if ($j<($num_fields-1)) { $return.= ','; }
					}
					$return.= ");\n";
				}
			}
			$return.="\n\n\n";
		}
	
		//Save .sql file
		$handle = fopen($name.'-backup-'.date('d-m-y-H-i-s').'.sql','w+');
		fwrite($handle,$return);
		fclose($handle);
	
		/*
		@ Use this code if you want to create zipped file
		*/
		/*
		$gzdata = gzencode($return, 9);
		$handle = fopen($name.'-backup-'.date('d-m-y-H-i-s').'.sql.gz','w+');
		fwrite($handle, $gzdata);
		fclose($handle);
		*/
    }
?>

 

We all know how important the database backup is, so don’t hesitate or waste your time in thinking else, have backup your database at a regular interval

Have backup, keep yourself in safe side, Have fun, Enjoy Scriptarticle!!