Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, July 5, 2023

Debian Bookworm: installing Apache2, MariaDB and PHP 8.2 (default)

 Installing MariaDB database

# apt-get install mariadb-client mariadb-server  

Install apache2 and PHP

# apt-get install apache2 php libapache2-mod-php php8.2-mysql

By default, PHP version for Debian Bookworm is 8.2.

To disable apache2 and mariadb start during boot (to faster your PC start, not recommended for live server)

# systemctl disable apache2
# systemctl disable mariadb

You can start and stop manualy after boot.

Install additional php 8.2 required by CodeIgniter

# apt-get install php8.2-curl php8.2-imagick php8.2-gd php8.2-intl php8.2-mbstring php8.2-memcache php8.2-memcached php8.2-redis php8.2-xml php8.2-phpdbg


Thursday, April 25, 2019

Codeigniter: query builder select, insert, update and delete

Query table:
        $this->result = $this->db->get('ca_brand',$mmax, 0);
        if ($this->result) {
            return true;
        } else {
            return false;
        }
Query table with like filter:
        $this->db->like('name', $keyword); // buildin mysql escape
        $this->result = $this->db->get('ca_brand',0, $mmax);
        if ($this->result) {
            return true;
        } else {
            return false;
        }
 Insert row in table:
        // buildin mysql escape character
        $data = array('name' => $name);
        $this->db->insert('ca_brand', $data); // return true on success
        $insert_id = $this->db->insert_id(); // get id

Update row in table for spesific id
        // buildin mysql escape character
        $id = intval($id);
        $data = array ('name' => $name);
        $this->db->where('id', $id);
        //$this->db->update('ca_brand',$data, "id = "$id ); // option 1
        $this->db->update('ca_brand',$data); // // option 2
        $numaffectedrow = $this->db->affected_rows(); // to get nummber affected row
 Delete row:
        $id = intval($id);
        $this->db->where('id', $id);
        $this->db->delete('ca_brand');
        $numaffectedrow = $this->db->affected_rows(); // to get nummber affected row

Php 7.2 and Codeigniter 3.1.10

Codeigniter: check mysql connection

To check error in mysql connection, we need to change ENVIRONMENT to Production. Here is the example:
    //define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
    //define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'testing');
    define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
There are two ways to catch error in our controller:
1. Using dblib
        $this->load->dbutil();
        if ($this->dbutil->database_exists('myca')) {
            // Connection ok
        } else {
            // Connection fail
        }
2. Checking error in db
        $this->load->database();
        $errhandle = $this->db->error();
        if ($errhandle['code']==0) {
            // Connection ok
        } else {
            // Connection fail
            // to dump error code var_dump( $this->db->error());
        }
Tested on PHP 7.2 and Codigniter 3.1.10

Thursday, October 13, 2016

PHP >= 5.5: password hashing

To hash password (using default algorithm bcrypt)

$mypass = "password";
$myhash = password_hash($mypass, PASSWORD_DEFAULT);

To verify password

$brutepass = "test";
password_verify ($brutepass, $myhash); // true or false

Storing password in database

"Therefore, it is recommended to store the result in a database column that can expand beyond 60 characters (255 characters would be a good choice)." Maybe varchar(255)

Reference:

Tuesday, October 11, 2016

PHP 5: GeoIP

Install PHP GeoIP

# apt-get install geoip-bin geoip-database geoip-database-extra php5-geoip php5-geos

Updating GeoIP database from SID (choose your mirror)

# wget http://kambing.ui.ac.id/debian/pool/main/g/geoip-database/geoip-database-extra_20160912-1_all.deb
# wget http://kambing.ui.ac.id/debian/pool/main/g/geoip-database/geoip-database_20160912-1_all.deb
# dpkg -i geoip-database_20160912-1_all.deb
# dpkg -i geoip-database-extra_20160912-1_all.deb  

To convert IP to integer

$ip = ip2long('119.249.54.66');

To convert integer to IP

$hostip = long2ip($ip);

To get 3 chars country code 

echo geoip_country_code3_by_name($hostip);

To get country name 

echo geoip_country_name_by_name($hostip);

To get country code and region

echo var_dump(geoip_region_by_name($hostip))."<br>";
 Error: mod_fcgid: stderr: PHP Warning:  geoip_region_by_name(): Required database not available at /usr/share/GeoIP/GeoIPRegion.dat. ??? May be required subscription premium service ??? 

Reference:

Monday, October 10, 2016

PHP >5.3: DateTime

Example construct Datetime object

$datetime = new DateTime("now");
$datetime = new DateTime('2000-01-01');

Example to print Datetime

echo $datetime->format('Y-m-d\TH:i:s');

Example to add a hour

$datetime->add(new DateInterval('PT1H'));

Example to add 10 day

$datetime->add(new DateInterval('P10D'));
Note:
  • P: Period
  • T: Time

Comparing 2 Datetime

$datetime = new DateTime('2016-10-10');
$datetime2 = new DateTime('2016-10-9');
echo var_dump($datetime > $datetime2).' $datetime > $datetime2 <br>'; // bool(true) $datetime > $datetime2 
echo var_dump($datetime < $datetime2).' $datetime < $datetime2 <br>'; // bool(false) $datetime < $datetime2 


References:

Wednesday, October 5, 2016

Simple Pagination With PHP and MySQL

This is my personal note to create simple pagination using php and mysql. I used mysqli object oriented style for future php support. Version MySQL and PHP are:
  • MySQL Version 5.5
  • PHP Version 5.6
To create your own demo, create your database and populate it.
CREATE TABLE `kci_logipv4` (
  `logdate` datetime DEFAULT NULL,
  `logipv4` int(11) unsigned DEFAULT NULL,
  `logmsg` varchar(1000) DEFAULT NULL,
  `logfrom` int(11) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `kci_logipv4` VALUES ('2016-09-28 23:22:11',3104583205,'SSH',1),('2016-09-28 23:22:11',3104583205,'SSH',1),('2016-09-29 09:07:50',1347119510,'SSH',1),('2016-09-29 09:07:51',1347119510,'SSH',1),('2016-09-29 20:28:27',3743330746,'SSH',1),('2016-09-29 20:28:28',3743330746,'SSH',1),('2016-09-29 20:59:51',1033072143,'SSH',1),('2016-09-29 20:59:53',1033072143,'SSH',1),('2016-09-29 21:06:55',1907273049,'SSH',1),('2016-09-29 21:06:57',1907273049,'SSH',1),('2016-09-30 03:20:08',3075559489,'SSH',1),('2016-09-30 03:20:09',3075559489,'SSH',1),('2016-09-30 11:47:19',1323303364,'SSH',1),('2016-09-30 11:47:21',1323303364,'SSH',1),('2016-09-30 21:59:33',3565240261,'SSH',1),('2016-09-30 21:59:35',3565240261,'SSH',1),('2016-10-01 03:09:18',1794631394,'SSH',1),('2016-10-01 03:09:18',1794631394,'SSH',1),('2016-10-01 10:02:35',2746017717,'SSH',1),('2016-10-01 10:02:36',2746017717,'SSH',1),('2016-10-01 10:29:18',1963359914,'SSH',1),('2016-10-01 10:29:19',1963359914,'SSH',1),('2016-10-01 10:57:30',2000423175,'SSH',1),('2016-10-01 10:57:31',2000423175,'SSH',1),('2016-10-01 21:33:06',1757971329,'SSH',1),('2016-10-01 21:33:07',1757971329,'SSH',1),('2016-10-02 21:00:31',1033072141,'SSH',1),('2016-10-02 21:00:34',1033072141,'SSH',1),('2016-10-03 02:35:10',2065638212,'SSH',1),('2016-10-03 02:35:11',2065638212,'SSH',1),('2016-10-03 06:18:41',3754290014,'SSH',1),('2016-10-03 06:18:43',3754290014,'SSH',1),('2016-10-03 15:14:53',1912501632,'SSH',1),('2016-10-03 15:14:53',1912501632,'SSH',1),('2016-10-04 00:04:44',3281659836,'SSH',1),('2016-10-04 00:04:45',3281659836,'SSH',1),('2016-10-04 01:45:32',1168426137,'SSH',1),('2016-10-04 01:45:33',1168426137,'SSH',1),('2016-10-04 12:04:05',2065638165,'SSH',1),('2016-10-04 12:04:05',2065638165,'SSH',1),('2016-10-04 13:11:16',457715326,'SSH',1),('2016-10-04 13:11:16',457715326,'SSH',1),('2016-10-04 16:50:40',1839072489,'SSH',1),('2016-10-04 16:50:40',1839072489,'SSH',1),('2016-10-05 09:20:48',1760484871,'',1),('2016-10-05 09:20:48',1760484871,'',1);
Here is the kci_logread.php code you can view it live
<?php

/*
database: database
table: kci_logipv4
logdate datetime
logipv4 int(11)
logmsg varchar(1000)
logfrom int
*/

// host, user, password, database, [port]
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
    die('Internal Server Error maybe database');
}

//This is the page number want to display
//retrieve from user navigation via method get or post
$page = 1;
if(isset($_GET["page"])){
    $page = intval($_GET["page"]); // sanitize input
}
if(isset($_POST["page"])){
    $page = intval($_POST["page"]); // sanitize input
}
//This is the number of results displayed per page
$page_rows = 15;

//count all result
//must equal to query to display data
$res = $mysqli->query("select Count(*) as total from kci_logipv4");
$rows = $res->num_rows;
//number of data available
$total=0;
if($rows) {
    $row = $res->fetch_assoc();
    $total = $row['total'];
}

//this is which first row should be retrieve
$start = ($page_rows * $page) - $page_rows;
//query to display data
//read $page_row data start from row $start 
$res = $mysqli->query("SELECT * FROM kci_logipv4 order by logdate desc Limit $start, $page_rows ");
//number of data to display
$rows = $res->num_rows;

//This tells us the page number of our last page
$last = ceil($total/$page_rows);

?>
<html>
<body>
<p>Server Farm Info</p>
<?php
if ($res) {
    echo "<p>Total: $total</p>";
    if ($rows>0) {
?>
<table border="1">
    <tr>
        <th>From</th>
        <th>Datetime</th>
        <th>IP</th>
        <th>Comment</th>
    </tr>
<?php
        while ($row = $res->fetch_assoc()) {
?>
    <tr>
        <td>Server farm <?php //echo $row['logfrom']; ?></td>
        <td><?php echo $row['logdate']; ?></td>
        <td align="center"><?php echo long2ip($row['logipv4']); ?></td>
        <td><?php echo $row['logmsg']; ?></td>
    </tr>
<?php
        }
    } else {
        echo "<p>No Data</p>";
    }
?>
</table>
<?php
    if ($last>1) {
        echo "<P>";
        if ($page>1) {
            echo "<a href=\"".htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")."?page=1\">First</a> ";
        }
        if ($page>2 && $page<=$last) {
            echo "... ";
        }
        if ($page>1) {
            echo "<a href=\"".htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")."?page=".($page-1)."\">".($page-1)."</a> ";
        }
        echo "$page ";
        if ($page+1<=$last) {
            echo "<a href=\"".htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")."?page=".($page+1)."\">".($page+1)."</a> ";
        }
        if ($page+1<$last) {
            echo "... ";
        }
        if ($page<$last) {
            echo "<a href=\"".htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8")."?page=$last\">Last</a> ";
        }
        echo "| Number of pages: $last </p>";
} else {
    echo "<p>Query Fail</p>";
}
?>
</body>
</html>

Wednesday, May 4, 2016

How to integrated web template into codeigniter 3.0.6 (with upgrade to 3.1.2)

Prerequistes:

After you extract codeigniter 3.1.2 you may have these structure directory:
[root codeigniter]
application
system
userguide
To upgrade codeigniter 3.0.6 to 3.1.2, extract codeigniter 3.1.2 and copy/replace system folder into [root codeigniter] folder. To upgrade from other version read this guide.
Extract your template into codeigniter folder [root codeigniter].
[root codeigniter]
application
system
userguide
fft--css
--fonts
--images
--js
--video
--index.html
--w3layouts-License.txt
Create file test.php file in folder controller.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
  public function __construct() {
    parent::__construct();
  }
  public function view($page = 'Test')
  {
    $this->load->helper('html');
    $this->load->helper('url');
    if ( ! file_exists(APPPATH.'views/fft/'.$page.'.php'))
    {
      show_404();
    }
    $data['title'] = $page; // Capitalize the first letter

    $this->load->view('fft/templates/pageheader',$data);
    $this->load->view('fft/'.$page, $data);
    $this->load->view('fft/templates/pagefooter');
  }
}

?>
At folder views, create folder fft.
application
--------bodymenu.php

--------pageheader.php
------test.php
We split file views->test.php. The other supporting files we put them into folder  [root codeigniter]->application->templates.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!-- banner -->
  <div data-vide-bg="<?php echo base_url(); ?>fft/video/sea">
    <div class="center-container">
      <div class="container">
        <!-- load body header -->
        <?php $this->load->view('fft/templates/bodyheader'); ?>
        <div class="logo animated wow zoomIn" data-wow-delay="900ms">
          <h1><a href="/web/joomla/index.html">Frequent <span>flyer</span><i>take me anywhere</i></a></h1>
        </div>
        <div class="start animated wow bounce" data-wow-delay="700ms">
          <a href="#about" class="hvr-bounce-to-bottom scroll">Get Started</a>
        </div>
      </div>
    </div>
  </div>
Final directory will like this:
[root codeigniter]
------views
------templates
--------bodyheader.php
--------pagefooter.php
------about.php
------test.php
--system
--userguide
--fft
----css
----fonts
----images
----js
----video
----index.html
----w3layouts-License.txt

You can download zip file from http://www.garasiku.web.id/fft.zip and extract it into [root codeigniter]

References: