Posts filed under 'Php'

The Apache configuration htpasswd, md5, mod_rewrite,

The Apache configuration file httpd.conf contains settings applied to all hosted domains, and also domain specific settings (settings contained in <VirtualHost> tags). Of course you don’t want your users to edit the httpd.conf file, but fortunately they can do site specific settings themselves by allowing them to use .htaccess files.

It’s easy…

A .htaccess file (note the dot in front of the filename) is a plain text file with settings in it. It can be placed at the root of a website or any sub directory. The settings in it will apply to the current and all sub directories in the current directory.

First of all, you must set the AllowOverride directive in httpd.conf. AllowOverrride controls which types of directives that are allowed in .htaccess files.:

<Directory “/usr/local/www/test.com”>
AllowOverride All
</Directory>

Add a section like the above for each of the domains you want to allow using .htaccess.

Remember to restart Apache to make the changes take effect.

The following sections of this page, describes som examples of using .htaccess files.

Note: Instruct your users to use ASCII mode when uploading .htaccess files. If BINARY mode is used the file will NOT work.
Password protection using a password file

This section describes how to protect all or part of a website against unauthorized access. The content of the .htaccess file:

AuthUserFile /usr/local/www/secure_directory/.htpasswd
AuthGroupFile /dev/null
AuthName “My protected site”
AuthType Basic

require valid-user

In the first line, replace bolded text with the path to your own protected area. In the third line, replace bolded text with a text of your choice. This text will appear in the login boks.

You may need to CHMOD the .htaccess file to 644 or (RW-R–R–). This makes the file usable by the server.

Create a directory just above your website root. This is where you will create your password file, and we don’t want this in a public directory.

For example, my password file is in /usr/local/www/secure_directory/

Now, you need to create the .htpasswd file. You can do it manually by putting each users username an password into it:

pas:ffff;

Each line must end with a line break, and there must be an empty line at the end of the file.

Another way of creating the password file is by using the htpasswd command, but this requires ssh access to the server.

htpasswd -c /usr/local/www/secure_directory/.htpasswd pas
New password:
Re-type new password:
Adding password for user pas

Next time you want to create a user, ommit the -c option (this is important to remember, or you will override the existing users/passwords in the file). For security reasons, passwords do not show op on the screen as you type.

This example shows how to change the password for an existing user (bold text is all in one line):

htpasswd -b /usr/local/www/secure_directory/.htpasswd pas fff
Updating password for user pas

Add comment October 20, 2008

function html to text

function htmltotext($html)
{
$tags = array (
0 => ‘~<h[123][^>]+>~si’,
1 => ‘~<h[456][^>]+>~si’,
2 => ‘~<table[^>]+>~si’,
3 => ‘~<tr[^>]+>~si’,
4 => ‘~<li[^>]+>~si’,
5 => ‘~<br[^>]+>~si’,
6 => ‘~<p[^>]+>~si’,
7 => ‘~<div[^>]+>~si’,
);
$html = preg_replace($tags,”\n”,$html);
$html = preg_replace(‘~</t(d|h)>\s*<t(d|h)[^>]+>~si’,’ – ‘,$html);
$html = preg_replace(‘~<[^>]+>~s’,”,$html);

$html = preg_replace(‘~ +~s’,’ ‘,$html);
$html = preg_replace(‘~^\s+~m’,”,$html);
$html = preg_replace(‘~\s+$~m’,”,$html);

$html = preg_replace(‘~\n+~s’,”\n”,$html);
return $html;
}

Add comment October 18, 2008

function error php

function error( $message )
{

echo ‘<div style=”color:red;font-weight:bold;font-size:12pt;font-family:monospace”>’.
“infobox::$message</div>”;

} // eof error()

Add comment October 18, 2008

Function php infoboxes with cookie

function infoboxes()
{
if( isset( $_COOKIE['infoboxes'] ))
{
if( $this->debug) echo $_COOKIE['infoboxes'];

$this->showinfoboxex = ( $_COOKIE['infoboxes'] == ‘ON’ );
}
}

Add comment October 18, 2008

Passing variables for a image between Javascript and PHP for web2

Try a javascript image object:

var myImage = new Image();
myImage.src = “http://mydomain.xxx/myfile.php?x=” +x+ “&y=” +y”;

then you can use $_GET[x]  in myfile.php to take the variable

Add comment September 22, 2008

Php script for email form

<?php
$fileatt
= ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment

$email_from = “”; // Who the email is from
$email_subject = “”; // The Subject of the email
$email_message = “”; // Message that the email has in it

$email_to = “”; // Who the email is too

$headers = “From: “.$email_from;

$semi_rand = md5(time());
$mime_boundary = “==Multipart_Boundary_x{$semi_rand}x”;

$headers .= “\nMIME-Version: 1.0\n” .
“Content-Type: multipart/mixed;\n” .
” boundary=\”{$mime_boundary}\”";

$email_message .= “This is a multi-part message in MIME format.\n\n” .
“–{$mime_boundary}\n” .
“Content-Type:text/html; charset=\”iso-8859-1\”\n” .
“Content-Transfer-Encoding: 7bit\n\n” .
$email_message . “\n\n”;

/********************************************** First File ********************************************/

$fileatt = “”; // Path to the file
$fileatt_type = “application/octet-stream”; // File Type
$fileatt_name = “”; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,‘rb’);
$data = fread($file,filesize($fileatt));
fclose($file);

$data = chunk_split(base64_encode($data));

$email_message .= “–{$mime_boundary}\n” .
“Content-Type: {$fileatt_type};\n” .
” name=\”{$fileatt_name}\”\n” .
//”Content-Disposition: attachment;\n” .
//” filename=\”{$fileatt_name}\”\n” .
“Content-Transfer-Encoding: base64\n\n” .
$data . “\n\n” .
“–{$mime_boundary}–\n”;
unset(
$data)
unset(
$file)
unset(
$fileatt)
unset(
$fileatt_type)
unset(
$fileatt_name)

/********************************************** Second File ********************************************/

$fileatt = “”; // Path to the file
$fileatt_type = “application/octet-stream”; // File Type
$fileatt_name = “”; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,‘rb’);
$data = fread($file,filesize($fileatt));
fclose($file);

$data = chunk_split(base64_encode($data));

$email_message .= “–{$mime_boundary}\n” .
“Content-Type: {$fileatt_type};\n” .
” name=\”{$fileatt_name}\”\n” .
//”Content-Disposition: attachment;\n” .
//” filename=\”{$fileatt_name}\”\n” .
“Content-Transfer-Encoding: base64\n\n” .
$data . “\n\n” .
“–{$mime_boundary}–\n”;
unset(
$data)
unset(
$file)
unset(
$fileatt)
unset(
$fileatt_type)
unset(
$fileatt_name)

/********************************************** End of File Config ********************************************/

// To add more files just copy the file section again, but make sure they are all one after the other! If they are not it will not work!

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo
“<font face=verdana size=2>The file was successfully sent!</font>”;
} else {
die(
“Sorry but the email could not be sent. Please go back and try again!”);
}
?>

Add comment August 13, 2008


Archives

Other

Categories

 

November 2009
M T W T F S S
« Oct    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Tags

.net 1.1 .net 2.0 .net 3.0 ADO.NET all-net-news alltechnews bsod c# c-sharp controls DataSet datasource dba dba-tool entity-framework Framework internet-land internet-life java-script jsf mozilla-foundation msbuild online-tools Page Layout php-development php-programming php5 plug-ins query script-land sql-add-on sql-backup-and-restore sql-data-storage sql-datetime sql-query sql-tips-and-tricks sqlauthority-news status-updates t-sql tech-land tech-review Visual Studio 2008 Web Browser Windows Server xquery