<?php
$file = ‘http://www.url.com/file.zip’;
$newfile = ‘newfile.zip’;
if (!copy($file, $newfile)) {
echo “failed to copy $file…\n”;
}
?>
// Create the function, so you can use it
function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// If the user is on a mobile device, redirect them
if(isMobile()) header("Location: http://m.yoursite.com/");
Options -IndexesHere is a simple script to take backup of MySQL database, using a PHP script.
<?php
backup_tables(‘localhost’,'username’,'password’,'dbname’);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$dbname,$tables = ‘*’)
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
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);
}
//cycle through
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] = ereg_replace(“\n”,”\\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 file
$handle = fopen(‘./db/db-backup-’.time().’-’.(md5(implode(‘,’,$tables))).’.sql’,'w+’);
fwrite($handle,$return);
fclose($handle);
}
?>
The code below will disable web browser’s default right-click context menu.
<script language=JavaScript>
var message=”Sorry, right click disabled!”;
///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){http://phphelp.co/wp-admin/post-new.php
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function(“alert(message);return false”)
</script>
Usually, default upload size from a web browser is 2 MB and if this is required to be changed, you can do it using .htaccess file.
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
Note: Some servers do not allow to change file upload size using .htaccess so Internal Server Error may appear in this case.
Sometimes we need to download files as attachment in PHP. This code can ideally be called on a hyperlink.
$filename = “myImage.jpg”;
if(file_exists($filename)) {
header(“Content-disposition: attachment; filename={$filename}”);
//Tell the filename to the browser
header(‘Content-type: application/octet-stream’);
//Stream as a binary file! So it would force browser to download
readfile($filename);
//Read and stream the file
}else{
echo “Sorry, the file does not exist!”;
}
Signup: Thank you very much for registering. Your account is now active and ready to use.
Contact Us: Thank you for contacting us. Your message has been sent to the concerned department and you will be contacted back shortly.
Profile Update: Your profile has been updated successfully.
Bug report submission message: Thank you very much for your time. Your reported bug/error has been routed to the concerned department for further action.
Lost Password: Your login information has been mailed to your email@domain.com.
Wrong Login info for Lost Password: Invalid login information; please provide the email address you used when you registered with us.
Lost Password Email:
Subject: Your SITE_NAME Login Information
Dear Member Name,
Your SITE_NAME login information is as below.
User Name: username
Password: password
The SITE_NAME Team
Registration Email to the member:
Subject: Welcome to SITE_NAME
Dear Member Name,
We welcome you to SITE_NAME. Your login information is as below and you can change your password anytime after logging in to the web site.
User Name: username
Password: password
The SITE_NAME Team
Watermarking an image in PHP is very easy. If you follow the code below, you can do it in 2 minutes.
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
//$SourceFile is source of the image file to be watermarked
//$WaterMarkText is the text of the watermark
//$DestinationFile is the destination location where the watermarked images will be placed
//Delete if destinaton file already exists
@unlink($DestinationFile);
//This is the vertical center of the image
$top = getimagesize($SourceFile);
$top = $top[1]/2;
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
//Path to the font file on the server. Do not miss to upload the font file
$font = ‘arial.ttf’;
//Font sie
$font_size = 16;
//Give a white shadow
$white = imagecolorallocate($image_p, 255, 255, 255);
imagettftext($image_p, $font_size, 0, 10, $top, $white, $font, $WaterMarkText);
//Print in black color
$black = imagecolorallocate($image_p, 0, 0, 0);
imagettftext($image_p, $font_size, 0, 8, $top-1, $black, $font, $WaterMarkText);
if ($DestinationFile<>”) {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header(‘Content-Type: image/jpeg’);
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>
<?php
$SourceFile = ‘image.jpg’;//Source image
$DestinationFile = ‘watermarked/image.jpg’;//Destination path
$WaterMarkText = ‘www.phpHelp.co’;//Watermark text
//Call the function to watermark the image
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
//Display watermarked image if desired
if(file_exists($DestinationFile)){
echo “<img src=\”watermarked/image.jpg\”>”;
echo “<p>The image has been watermarked at ‘”.$DestinationFile.”‘</p>”;
}
?>
Note: