Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
This is very simple artical for submit HTML form data without refreshing page with the help of JQuery and Ajax.
jQuery provides a rich set of methods for AJAX web development.
With jQuery AJAX, you can request TXT, HTML, XML or JSON data from a remote server using both HTTP Get and HTTP Post.

$.ajax(options) is the syntax of the low level AJAX function.
$.ajax offers more functionality than higher level functions like load, get, and post.
The option parameter takes name|value pairs defining url data, passwords, data types, filters, character sets, timeout and error functions.

Below is code for HTML form.
<form method="post" name="form">
<ul>
<li><input id="name" name="name" type="text" /></li>
<li><input id="username" name="username" type="text" /></li>
<li><input id="password" name="password" type="password" /></li>
<li><select id="gender" name="gender">
<option value="">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</li>
</ul>
<div >
<input type="submit" value="Submit" class="submit"/>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div>
</form>

Javascript code for jquery ajax form submit

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var username = $("#username").val();
var password = $("#password").val();
var gender = $("#gender").val();
var dataString = 'name='+ name + '&username=' + username + '&password=' + password + '&gender=' + gender;

if(name=='' || username=='' || password=='' || gender=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}else{
$.ajax({
type: "POST",
url: "result.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();}
});
}

return false;
});
});
</script>

Reference: http://www.justwebdevelopment.com
My PHP files all display a 500 Internal server error. How can I fix this?
If you are running a PHP-based script on your site and are receiving a 500 and/or 404 errors on your pages, it is likely you have one or more of the following occurring:
1. You are attempting to access PHP files through the temp-URL http://server.lunarpages.com/~username. PHP on our servers requires a fully qualified domain name. You may wait until your domain has been pointed and resolves here or you may email support@lunarpages.com to request a primary domain change to a free subdomain of lunarpages to test your files.
2. The permissions on some of the folders or files are 777 or 666. If this is the case, change them to either 755 or 644 in Cpanel's File Manager (or using your local FTP client).
3. The files and/or folders are not owned by you. Certain applications having been run under php as an apache module may have files owned by the apache user of nobody. An indication that you don't own the files would be if you are unable to change their file permissions. To correct this, please contact support provide your username and the last 4 digits of your credit card on file with us for ownership verification (no charges apply) and provide the location of the folder or files that need to have your ownership.
4. Your .htaccess file has php_values or php_flags in it. This causes a 500 Internal server error when attempting to execute the script.
The php_values and php_flags will need to be removed from your .htaccess file (please make a backup of the .htaccess by copying its contents and saving it on your desktop as htaccess.txt). Take the contents removed from .htaccess and place it into a file you create called php.ini. Remember to remove the php_flag and php_value part before the directives as php.ini files do not require those in front of the values. You can always make the changes and ask us if the changed files are correct.
Because php.ini values are not shared across directories, you would need a separate php.ini file in each folder that has .htaccess or that requires the php_values or php_flags. In order to avoid doing this, you can place a line in the .htaccess file in your public_html folder to have all values in your public_html php.ini to be shared across all folder. This line would be the following:
suPHP_ConfigPath /home/username/public_html
Finally, to explain in depth why suPHP requires these changes to the file permissions, please note that suPHP runs scripts with the permissions of their owners. Regular PHP executes scripts under the permissions of the system user running the web server, which means that your script runs with different permissions than your own user account and makes it very hard to use a PHP script to modify and create files without giving everyone on the server access to your files (this means that on regular PHP you provide write or execute access to group and world even for some files). Since SuPHP makes your PHP scripts run with the same permissions as your regular user account, you do not need group or world write access or execute access for files and suPHP will even prevent files from running that are group or world writable or executable as a security precaution.
666 equals the following:
Code:
Mode User Group World
Read 4 4 4 (all checked)
Write 2 2 2 (all checked)
Execute(none checked)
This makes group and world able to write to the file, a security risk
777 equals the following:
Code:
Mode User Group World
Read 4 4 4 (all checked)
Write 2 2 2 (all checked)
Execute 1 1 1 (all checked)
This makes group and world able to write and execute the file, a very large security risk.
Basically, suPHP is more secure, and preventing scripts from running as 666 or 777 prevents group or world from maliciously writing to the files and hacking your scripts.

Following code will retrieve all file names from a specified folder.

$filePath = "/var/www/public_html/mysite/images";/* Enter path to the folder */

$string="";

$fileCount=0;

 

$dir = opendir($filePath);

while ($file = readdir($dir)) {

if (eregi("\.png",$file)) { /* Look for files with .png extension */

$string .= "$file
";

$fileCount++;

}

}

if ($fileCount > 0) {

echo sprintf("
<h2>
All Files in %s></h2>

%sTotal Files: %s",$filePath,$string,$fileCount);

}

?>

Reference:http://www.snilesh.com

How Convert
<p>This is some text and here is a <strong>bold 
text then the post stop here....</p>

To
<p>This is some text and here is a 
<strong>bold text then the post stop here....</strong></p>
 
Follow the Code
 
function closetags($html) {
    preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)
(?: .*)?(?#iU', $html, $result);
    $openedtags = $result[1];
    preg_match_all('##iU', $html, $result);
    $closedtags = $result[1];
    $len_opened = count($openedtags);
    if (count($closedtags) == $len_opened) {
        return $html;
    }
    $openedtags = array_reverse($openedtags);
    for ($i=0; $i < $len_opened; $i++) {
        if (!in_array($openedtags[$i], $closedtags)) {
            $html .= '.$openedtags[$i].'>';
        } else {
            unset($closedtags[array_search($openedtags[$i], 
 $closedtags)]);
        }
    }
    return $html;
} 
Use this script to protect your contact form, your whois query tool or just a form where some extra validation is needed. How does it work?
A session will be created inside a dynamic image file (requires GD library). The random value of this image appears inside the generated CAPTCHA image. The user has to enter this value into a form field. While processing the form, the entered value get checked against the session value. Without entering the correct string a form will not be processed. This mechanism is very useful to protect your form again (spam) bots.

The PHP code snippet


Create a php file with that code an call it "random.php"
session_start();

if (empty($_SESSION['rand_code'])) {
    $str = "";
    $length = 0;
    for ($i = 0; $i < 4; $i++) {
        // this numbers refer to numbers of the ascii table (small-caps)
        $str .= chr(rand(97, 122));
    }
    $_SESSION['rand_code'] = $str;
}

$imgX = 60;
$imgY = 20;
$image = imagecreatetruecolor(60, 20);

$backgr_col = imagecolorallocate($image, 238,239,239);
$border_col = imagecolorallocate($image, 208,208,208);
$text_col = imagecolorallocate($image, 46,60,31);

imagefilledrectangle($image, 0, 0, 60, 20, $backgr_col);
imagerectangle($image, 0, 0, 59, 19, $border_col);

$font = "VeraSe.ttf"; // it's a Bitstream font check www.gnome.org for more
$font_size = 10;
$angle = 0;
$box = imagettfbbox($font_size, $angle, $font, $_SESSION['rand_code']);
$x = (int)($imgX - $box[4]) / 2;
$y = (int)($imgY - $box[5]) / 2;
imagettftext($image, $font_size, $angle, $x, $y, $text_col, $font, $_SESSION['rand_code']);

header("Content-type: image/png");
imagepng($image);
imagedestroy ($image);
?>

How-to use it?

Use this HTML code in your form:


CAPTCHA image

This is the code to test the entered value:
    // process your form here
    // at least destroy the session
    unset($_SESSION['rand_code']);
}
?>