Pages

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Using Google No captcha Re captcha in your web page 2016

<?php

if(isset($_POST['submit']))
{
$secret = "XXXXXXXXXXXXXXXXXXXXX"; //secret key

$response = $_POST['g-recaptcha-response']; // captcha response

$remoteip = $_SERVER['REMOTE_ADDR']; // get user ip

$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip"); // pass values to recapcha API

$result = json_decode($url, TRUE);

if($result['success']==1)
{
echo "SUCCESS"
}
else{
echo "failed"
}

}

php keep form values after submit

<input id="user_name" type="text" pattern="[a-zA-Z0-9]{2,64}" name="user_name" required class="form-control" value="<?php if(isset($_POST['user_name'])) { echo htmlentities ($_POST['user_name']); }?>" />

PHP Display date and time now according to your country (Server time change)

<?php 
       
date_default_timezone_set('Asia/Colombo'); //your country region

http://php.net/manual/en/timezones.php

$timenow = date('H:i:s', time());

$datenow = date('Y-m-d');

?>

PHP OOP PDO Database class example

Read / fetch data with PHP PDO Database Connection Class in Simple way

<?php
class db {
    private $username = "root";
    private $password = "";
    function connect() {
        $username = $this->username;
        $password = $this->password;
        try {
            $conn = new PDO('mysql:host=localhost;dbname=test', $username, $password);
        } catch (Exception $ex) {
            die($ex->getMessage());
        }
        return $conn;
    }
}

class getdata extends db {
    function read() {
        $con = $this->connect();
        $sql = "SELECT * FROM CITY";
        try {
            $citylist = $con->query($sql);
        } catch (Exception $ex) {
        }
        return $citylist;
    }
}

$getdataCity = new getdata();
$citylist = $getdataCity->read();

$a = $citylist->fetchAll();

print_r($a);

if($citylist->rowCount()){
    foreach ($a as $city){
        echo $city['city_name'];
    }
}

Filter numbers only from string PHP

Filter numbers only from string PHP $_get method


$itmidx = $_GET['item'];

$itmid = preg_replace('/[^0-9.]+/', '', $itmidx);

How to Protect PHP include files

Add this .htaccess file to your includes of the views folder of your PHP project to Protect  include files. This file prevents that your .PHP view files are accessed directly from the outside

How to Protect PHP include files

CODE

<Files ~ "\.(htaccess|php)$">
order allow,deny
deny from all
</Files>

Search This Blog