Archive

Archive for the ‘PHP’ Category

To disable the safe mode for particular domain

October 8th, 2009

You cannot turn safe_mode off in .htaccess.

You can only change this value in system files (root access needed); php.ini or in <virtualhost> entry in apache’s httpd.conf.
More info: http://www.php.net/features.safe-mode

HOW-TO:
Changing safe_mode to off (or on) in just one domain

edit /usr/local/apache/conf/httpd.conf
find something similar to:
Quote:
<VirtualHost IP.IP.IP.IP>
ServerAlias www.domain.com domain.com
ServerAdmin webmaster@domain.com
DocumentRoot /home/user/public_html
(…)
</VirtualHost>
of the domain name you wish to change, and palce inside <virtualhost></virtualhost>:
<IfModule mod_php4.c>
php_admin_flag safe_mode off (or on)
</IfModule>

That way, domain.com will have safe_mode disabled (or enabled)

PHP

Custom Session folder

October 8th, 2009

You can change session save path to custom folder.

Example:

1) Created “sessions” folder in public_html with 777 permissions.
2) edit .htaccess file in public_html folder with following line
php_value session.save_path /home/truthf70/public_html/sessions/

3) Created simple php file with code and test it.

Quote:
<?php
$s_path = session_save_path();
echo $s_path;
?>


Now session path set to “sessions” folder.

PHP

File uploading using php when safe mode is ON

October 8th, 2009

have writin a basic php script to upload files onto my web server but I am getting this error:

Warning: SAFE MODE Restriction in effect. The script whose uid is 519 is not allowed to access /var/www owned by uid 0 in /home/virtual/site8/fst/va

r/www/html/Admins/Admins/upload.php on line 14
Error: could not make directory.

Here is my code:

<?
session_start();

if (!session_is_registered(”SESSION”))
{
header(”Location: http://www.mywebsite.com/Admins/index.php“);
}
$dirname = “files”;
define(PATH, “/var/www/”);
$filename = $myfile_name;
if ($filename != “”)
{
if (!$dir = @opendir(PATH.$dirname))
mkdir(PATH.$dirname, 0700) or die(”<b>Error:</b> could not make directory.”);
copy($myfile, PATH.$dirname.”/”.$filename) or die(”<b>Error:</b> could not copy file.”);
unlink($myfile) or die(”<b>Error:</b> could not delete uploaded file.”);
echo “Upload sucessful.”;
}
else
{
echo “You must select a file to upload.”;
}
?>
<title>MyWebSite Administration</title>
<div align=”center”><B><font size=”+5″>MyWebSite File Upload</font></B>
</div>
<form action=”upload.php” enctype=”multipart/form-data” method=”POST”>
<div align=”center”>    <br />
<table width=”335″ border=”1″>
<tr>
<td width=”97″>File Directory:</td>
<td width=”222″><input name=”myfile” type=”file” id=”myfile”></td>
</tr>
<tr>
<td colspan=”2″><div align=”center”>
<input type=”submit” name=”submit2″ value=”Submit”>
</div></td>
</tr>
</table>
<input type=”hidden” name=”submit” value=”Submit”>
</div>
</form>
<p align=”center”><a href=”http://www.mywebsite.com/Admins/Admins/main.php“>Return to Admin Menu</a> | <a href=”http://www.mywebsite.com/Admins/main.php“>Return to Main Menu</a> </p>

There some more things I need added to the script.
1) Choose the directory to upload the file, but has the default path of http://www.mywebsite.com/files/
2) Have the php script print out all the files in that directory and have the option to delete and rename
3) When a file has sucessfuly uploaded, “Upload Complete” prints out under my title in red font.

I’m happy to give an A grade to anyone who can help me with my code.

PHP

How to set register_globals OFF

October 8th, 2009

You can create .htaccess file in public_html folder and add following code to set Register_globals to OFF for one domain.

php_flag register_globals off

Or

You can add above code to virtual host entry in httpd.conf and restart apache.

PHP

How to run a PHP script on cron?

October 8th, 2009

Please go through following URL to setup PHP script as cron job.

Look here to see how to setup your own crontab.

If your PHP scripts do not have executable permissions, 644 or -rw-r–r– for example, then as part of the command portion of the cron line, you must specify the php interpreter and pass it the filename of the PHP script (including full path to the script from your home directory) that you want executed, like so:

28 14 * * * /usr/local/bin/php /myscript.phtml
6 3 20 4 * /usr/local/bin/php /htdocs/www/x.php

The first cron line above will run myscript.phtml located in your home directory every day at 2:28PM. The second line will run the script x.php from your /htdocs/www/ directory once a year on April 20th at 3:06AM.

When you explicitly specify the php interpreter /usr/local/bin/php your scripts need not have filenames ending in .php .phtml .php3 .php4. Conversely, if your script filenames do not end in one of those PHP extensions, then you must explicitly use the php interpreter in the command portion of your cron as above.

If your PHP scripts do have executable permissions like 755 or -rwxr-xr-x and they have one of the PHP filename extensions above, then you do not need to specify the php interpreter in the command portion of your cron line, like this:

5 17 * * 2 /myscript.php

The above cron would run myscript.php in your home directory every Tuesday at 5:05PM.

PHP

How do I set PHP include_path?

October 8th, 2009

Here you can see how to setup include_path for some customer who needs use PEAR modules.

How do I set PHP include_path?

There are at least 2 ways you can set your PHP include_path.

1. Edit your /etc/php.ini file. Inside that file is a directive section for “Paths and Directories.” By default it is set to “.:/usr/local/lib/php”. You can change that value to whatever you like or add to (or eliminate) the default like this:

include_path = .:/usr/local/lib/php:./include

The above must be entered all on one line, with no linebreaks. The above will cause php scripts to search for a directory named “include”, and will look for it under the current directory of the running script in addition to the default, which is to first search the current directory and then search the /usr/local/lib/php directory (which is a system directory that is not writeable by you and is reserved for things like PEAR).

The format of the include_path variable is a list of directories separated with a colon. A “.” (a period) in the include path means the current directory, and allows for relative includes. More information is available here.

2. Use the function ini_set(). For example if you wanted to set your PHP include_path to “.:../:./include:../include” then you would do this in your PHP code:

ini_set(”include_path”, “.:../:./include:../include”);

You can also use ini_set() to affect other PHP settings like auto_prepend_file, auto_append_file, error_reporting, etc.

3. This last option does not apply to all users. Only if you have specifically requested the Apache module of PHP, then instead of editting the php.ini file, you would make an .htaccess file in the directory that you want to set the include_path for, and inside that file put the following on one line:

php_value include_path .:../:./include:../include

The above must be entered all on one line, with no linebreaks. All directories below that one will be similarly affected unless you make an .htaccess file inside them spcifying a different include_path.

PHP

Can I have all .html pages parsed as PHP?

October 8th, 2009

By default, files ending in .php, .php3, .php4 and .phtml will be parsed as PHP and you do not need to do anything to make this happen.

If you want all .html files parsed as PHP, just add the following line to an .htaccess file in the same directory or any directory above the one in which you want this behavior:

AddHandler cgi-script .html

To also have all .htm files parsed as PHP, add .htm to the end of the above line. Nothing else is required if you want .htm or .html files to be parsed as PHP.

If you want any other filetype other than .htm or .html to be parsed as PHP, such as .wow then you will have to do as above:

AddHandler cgi-script

PHP

How to send email through php ?

October 8th, 2009

Send email using the PHP mail() function

Description
How to send an email from within a PHP page using the built in mail() function.
The code

<?php

// Your email address
$email = “you@example.com”;

// The subject
$subject = “Enter your subject here”;

// The message
$message = “Enter your message here”;

mail($email, $subject, $message, “From: $email”);

echo “The email has been sent.”;

?>

PHP

Using PHP mail funtions

October 8th, 2009

You can send email simple php mail() function with out using cgi scripts. B)

Simple PHP mail script :

This script allows you to place a simple form for sending emails on any HTML page. The script shows you how to gather user input, perform form validation with PHP, and send an email.

First, make the form page mail.html (you may call it whatever you like)…

Quote:
<html>
<head><title>Mail sender</title></head>
<body>
<form action=”mail.php” method=”POST”>
<b>Email</b><br>
<input type=”text” name=”email” size=40>
<p><b>Subject</b><br>
<input type=”text” name=”subject” size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name=”message”></textarea>
<p><input type=”submit” value=” Send “>
</form>
</body>
</html>

The form contains the necessary text fields Email, Subject, Message, and the Send button. The line
<form action=”mail.php” method=”POST”>
tells the browser which PHP file will process the form and what method to use for sending data.

When the user fills in the form and hits the Send button, the mail.php file is called…

Quote:
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php

/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];

/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn’t empty. preg_match performs a regular expression match. It’s a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match(”/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/”, $email)) {
echo “<h4>Invalid email address</h4>”;
echo “<a href=’javascript:history.back(1);’>Back</a>”;
} elseif ($subject == “”) {
echo “<h4>No subject</h4>”;
echo “<a href=’javascript:history.back(1);’>Back</a>”;
}

/* Sends the mail and outputs the “Thank you” string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo “<h4>Thank you for sending email</h4>”;
} else {
echo “<h4>Can’t send email to $email</h4>”;
}
?>
</body>
</html>

As you see, the script is simply one if … elseif … else statement. At first, it validates the required form fields. Note that PHP form validation is performed on the server, after sending all the data. Therefore, it would be a good idea to combine server-side form validation with PHP and client-side form validation with JavaScript in order to avoid unnecessary data sending.

If the email address is valid and subject isn’t empty, the script sends the mail and displays the corresponding message. Note how the variable $email is included into the output string.

You can also use this script to implement the safe “Contact Us” function on your website. Your visitors will be able to send you a message, but your email address won’t be displayed on the page and spam bots, that parse pages looking for potential email addresses, won’t get it.

Just remove the Email text field from the form and replace the first line of the script with something like…
$email = ‘YourAddr@YourMail.com’;
And, of course, you don’t need to validate the email address in this case.

PHP

PHP Configuration Info

October 8th, 2009

Want to see complete PHP configuration on a server and installed modules.

Copy following code to notepad and save file as phpinfo.php :

<? phpinfo(); ?>

Upload phpinfo.php to any domain public_html folder on server to see PHP configuration information.

PHP