jueves, 24 de diciembre de 2020

setting time zone php

 <?php

date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();
echo "<br>";
echo time();

echo "<br>";
echo date('r');
?>

miércoles, 18 de noviembre de 2020

Remove newlines using regex.

 //Replace the newline and carriage return characters

//using regex and preg_replace.

$text = preg_replace("/\r|\n/", "", $text);


remove \r\n 

viernes, 23 de octubre de 2020

php crud

 

https://www.studentstutorial.com/php/php-crud

https://drive.google.com/file/d/14579xTbSKyI3ZSJHfMSRSPBwpN8oJooC/view?usp=sharing

miércoles, 7 de octubre de 2020

google speech library

 require google/cloud-speech


https://cloud.google.com/speech-to-text/docs/quickstart-client-libraries#client-libraries-install-php

Send JSON data via POST with PHP cURL

 

The following example makes an HTTP POST request and send the JSON data to URL with cURL in PHP.

  • Specify the URL ($url) where the JSON data need to be sent.
  • Initiate new cURL resource using curl_init().
  • Setup data in PHP array and encode into a JSON string using json_encode().
  • Attach JSON data to the POST fields using the CURLOPT_POSTFIELDS option.
  • Set the Content-Type of request to application/json using the CURLOPT_HTTPHEADER option.
  • Return response as a string instead of outputting it using the CURLOPT_RETURNTRANSFER option.
  • Finally, the curl_exec() function is used to execute the POST request.
// API URL
$url 'http://www.example.com/api';

// Create a new cURL resource
$ch curl_init($url);

// Setup request to send json via POST
$data = array(
    
'username' => 'codexworld',
    
'password' => '123456'
);
$payload json_encode(array("user" => $data));

// Attach encoded JSON string to the POST fields
curl_setopt($chCURLOPT_POSTFIELDS$payload);

// Set the content type to application/json
curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Type:application/json'));

// Return response instead of outputting
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute the POST request
$result curl_exec($ch);

// Close cURL resource
curl_close($ch);

Receive JSON POST Data using PHP

The following example shows how you can get or fetch the JSON POST data using PHP.

  • Use json_decode() function to decoded JSON data in PHP.
  • The file_get_contents() function is used to received data in a more readable format.
$data json_decode(file_get_contents('php://input'), true);

miércoles, 30 de septiembre de 2020

php postgre

 install dependencies  sudo apt-get install php7.0-pgsql

yum install php-pgsql

restart apache

https://stackoverflow.com/questions/38981799/unable-to-install-php5-pgsql-on-ubuntu-16-04/42772824


<?php

   $host        = "host = hus-west-2.rds.amazonaws.com";

   $port        = "port = 5432";

   $dbname      = "dbname = product";

    $pass="dElm0zL";

$pid=$argv[1];

    $credentials = "user=phone  password=$pass";


   $db = pg_connect( "$host $port $dbname $credentials"  );

   if(!$db) {

      echo "Error : Unable to open database\n";

   } else {

      echo "Opened database successfully\n";

   }


   $sql =<<<EOF

      SELECT * FROM public.howardmillerinventory where "Clock"='$pid'

EOF;


   $ret = pg_query($db, $sql);

   if(!$ret) {

      echo pg_last_error($db);

      exit;

   }

   while($row = pg_fetch_row($ret)) {

      echo "Item# = ". $row[0] . "\n";

      echo "Stock= ". $row[1] ."\n";

      }

   echo "Operation done successfully\n";

   pg_close($db);

?>








domingo, 20 de septiembre de 2020

mysql export to csv

 https://makitweb.com/how-to-export-mysql-table-data-as-csv-file-in-php/

https://www.itechempires.com/2016/05/export-data-from-mysql-to-csv-using-php/

jueves, 17 de septiembre de 2020

php iptables

run   sudo visudo



root  ALL=(ALL) ALL


wwwrun ALL=NOPASSWD: /usr/sbin/iptables



<?php



$ip=$_SERVER['REMOTE_ADDR'];
echo " Welcome your IP $ip has been white listed";



 $web=shell_exec("sudo iptables -I INPUT 2 -m state --state NEW,RELATED,ESTABLISHED -m tcp -p tcp --dport 80 -s $ip -j ACCEPT");

 $sip=shell_exec("sudo iptables -I INPUT 2 -m state --state NEW,RELATED,ESTABLISHED -m udp -p udp --dport 28614 -s $ip -j ACCEPT"
?>

https://exain.wordpress.com/2007/11/24/execute-system-commands-via-php/

sábado, 25 de julio de 2020

mysql insert


<?php
$link = mysqli_connect("localhost", "root", "2224psss", "dialer");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "INSERT INTO `audio` (`audio_id`, `audio_name`, `audio_desc`, `audio_path`, `audio_size`, `audio_date`)
VALUES (NULL, 'audio_name', 'Audio desc', 'path', '100MB', CURRENT_TIMESTAMP)";
if(mysqli_query($link, $query)) {
printf ("New Record has id %d.\n", mysqli_insert_id($link));
}
else {
printf("Error: %s\n", mysqli_error($link));
}
/* close connection */
mysqli_close($link);
?>

martes, 21 de julio de 2020

IP list from a file

<?php
$ip=file_get_contents("http://149.248.59.112/ip_address.txt");
$ip=explode("\n",$ip);

foreach($ip as $value){
if(!empty($value)){
if (in_array("$value", $ip)) {
    echo "Got $value<br>";
}

else {

echo " I\'m sorry";
}

}

}
?>


196.179.2.65
154.104.154.150
102.158.71.137
196.179.45.12
102.156.151.182
196.179.27.230
41.227.238.97

domingo, 12 de julio de 2020

How to Reset Joomla Admin Password?

In ordinary way you can reset Joomla admin password from the admin panel through the User Manager. To do this, you have to be logged in as a Super Administrator.

What if it is impossible to reset Joomla admin passwords in such a way? For example, your website have been hacked, the previous admin person is no longer available or you just have forgotten the password. What you have to do?

Joomla is not simple thing. Joomla has some possibilities to reset admin password in other way. We will show you two methods, follow our instructions and get knowledge in how to reset Joomla admin password:

lunes, 23 de marzo de 2020

PHP sendgrid API

 1007  echo "export SENDGRID_API_KEY='SG.8J2caHDtU'" > sendgrid.env


 1008  echo "sendgrid.env" >> .gitignore

 1009  source ./sendgrid.env

 1013  composer sendgrid/sendgrid

 1014  sudo curl -sS https://getcomposer.org/installer | php

 1015   mv composer.phar /usr/local/bin/composer

 1016  composer -V

 1023  composer require sendgrid/sendgrid
---------------------------------------------------------------------
PHP CODE

    <?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

$email = new \SendGrid\Mail\Mail();
$email->setFrom("admin@asterisk-voip.com", "Ambiorix Rodriguez");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("ambiorixg12@gmail.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

?>

viernes, 6 de marzo de 2020

<?php
/* formats a 10 or 7 digit phone number (number only) by adding dashes at the appropriate locations */

function phoneFormat($number) {
if(ctype_digit($number) && strlen($number) == 10) {
  $number = substr($number, 0, 3) .'-'. substr($number, 3, 3) .'-'. substr($number, 6);
} else {
if(ctype_digit($number) && strlen($number) == 7) {
$number = substr($number, 0, 3) .'-'. substr($number, 3, 4);
}
}
return $number;
}

echo  phoneFormat(8297243489);

?>

829-724-3489

lunes, 10 de febrero de 2020

login script

<?php


  $dbhost='localhost';
        $dbuser='root';
        $dbpass=789456;
        $dbname='codigos';

        $db = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Couldn't connect to the database.");

        mysqli_select_db($db,$dbname) or die("Couldn't select the database");



    //--------------- AUTHENTICATION MODULE --------------------------

        function displayLogin() {
                header("WWW-Authenticate: Basic realm=\"Ambiorix\"");
                header("HTTP/1.0 401 Unauthorized");
                echo "<h2>Authentication Failure</h2>";
                echo "The username and password provided did not work. Please reload this page and try again.";
                exit;
        }



        if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
                // If username or password hasn't been set, display the login request.
                displayLogin();
        } else {
                // Escape both the password and username string to prevent users from inserting bogus data.
                $PHP_AUTH_USER = addslashes($_SERVER['PHP_AUTH_USER']);
                //$PHP_AUTH_PW = md5($PHP_AUTH_PW);
                $PHP_AUTH_PW = $_SERVER['PHP_AUTH_PW'];

                // Check username and password against the database.
                   $query="SELECT count(user_id) as valid FROM codeusers WHERE password='$PHP_AUTH_PW' AND user='$PHP_AUTH_USER' ";    
                  $result = mysqli_query($db, $query);
                  while ($row = mysqli_fetch_assoc($result)) {
        if($row[valid]<1)  {
                displayLogin();

}


}
}
?>


CREATE TABLE `codeusers` (
  `user_id` int(10) NOT NULL,
  `user` char(10) NOT NULL,
  `password` char(10) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

domingo, 2 de febrero de 2020

Display and hide HTML elements

JQUERY
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("input[name=btnPassport]").click(function () {
                if ($(this).val() == "Yes") {
                    $("#dvPassport").show();
                } else {
                    $("#dvPassport").hide();
                }
            });
        });
    </script>
    <span>Do you have Passport?</span>
    <input type="button" value="Yes" name="btnPassport" />
    <input type="button" value="No" name="btnPassport" />
    <hr />
    <div id="dvPassport" style="display: none">
        Passport Number:
        <input type="text" id="txtPassportNumber" />
    </div>
</body>
</html>

JAVASCRIPT


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        function ShowHideDiv(btnPassport) {
            var dvPassport = document.getElementById("dvPassport");
            dvPassport.style.display = btnPassport.value == "Yes" ? "block" : "none";
        }
    </script>
    <span>Do you have Passport?</span>
    <input type="button" value="Yes" onclick="ShowHideDiv(this)" />
    <input type="button" value="No" onclick="ShowHideDiv(this)" />
    <hr />
    <div id="dvPassport" style="display: none">
        Passport Number:
        <input type="text" id="txtPassportNumber" />
    </div>
</body>
</html>

lunes, 6 de enero de 2020

json array object

<?php


$data=array(array("name"=>"Ambiorix","Lastname"=>"Rodriguez","age"=>21));


$info=json_encode($data);

print_r("{info : ".$info."}");

?>

MYSQL to json Object array

<?php
$link = mysqli_connect("localhost", "root", "", "asteriskcdrdb");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$cars=array();

$query = " select * from  cdr limit 5";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        echo "$row[uniqueid], $row[src]<br>";
      $row[uniqueid]=array("src"=>$row[src],"dst"=>$row[dst]);

$cars[]=$row[uniqueid];


   }

    /* free result set */
    mysqli_free_result($result);
}

$info=json_encode($cars);

print_r($info);

//else {

// printf("Error: %s\n", mysqli_error($link));

//}


/* close connection */
mysqli_close($link);
?>


[{"src":"+16145993800","dst":"s"},{"src":"+16145993800","dst":"600"},{"src":"+16145993800","dst":"600"},{"src":"100","dst":"500"},{"src":"+16145993800","dst":"600"}]

domingo, 5 de enero de 2020

creating json array object

<?php


$cars=array(array("name"=>"Ambiorix","lastname"=>"Rodriguez","age"=>38));


$info=json_encode($cars);

print_r($info);

?>

[{"name":"Ambiorix","lastname":"Rodriguez","age":38}]

viernes, 3 de enero de 2020

allow numbers

106
You can use preg_replace in this case;
$res = preg_replace("/[^0-9]/", "", "Every 6 Months" );
$res return 6 in this case.
If want also to include decimal separator or thousand separator check this example:
$res = preg_replace("/[^0-9.]/", "", "$ 123.099");
$res returns "123.099" in this case
Include period as decimal separator or thousand separator: "/[^0-9.]/"
Include coma as decimal separator or thousand separator: "/[^0-9,]/"
Include period and coma as decimal separator and thousand separator: "/[^0-9,.]/"