Category Archives: programming

Prevent form submitted twice

Sometimes there is issue we got double records in the database due to the form submitted twice.

Here is the way how to prevent it. A simple way

session_start();    
if (!$_SESSION['post_data'] == $_POST)
{
// process the data
// after success process, set the session to post value
    $_SESSION['post_data'] = $_POST;
}
else
{
// display a message that the form already submitted. 
}

Telegram bot to send message

This is a simple code for a telegram bot to send message to a person or to a group

function send_telegram($telegram_id, $message_text) {

        $secret_token='123456789:adfjalJNhahasdfHUsQ';

        // to get group telegram id (can be in negative value)
        // https://api.telegram.org/bot1228803793:AAEElKIr5OhBlnMpsvPVNLa_Gb4cjSriUsQ/getUpdates

        $url = "https://api.telegram.org/bot" . $secret_token . "/sendMessage?parse_mode=markdown&chat_id=" . $telegram_id;
        $url = $url . "&text=" . urlencode($message_text);
        $ch = curl_init();
        $optArray = array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true
        );
        curl_setopt_array($ch, $optArray);
        $result = curl_exec($ch);
        curl_close($ch);
    }

Some important notes

  1. You need to create a bot using @botFather (search this bot in telegram and just follow the steps to create your own bot)
  2. Once your bot successfully created, you need to capture the secret token given
  3. To get the telegram id for user – can ask the user to send a message to @userinfobot – the telegram id is in integer
  4. To get telegram id for a group, you need to add the bot to the group and go to this link to get the group id, group id start with – (dash/negative sign)
    https://api.telegram.org/bot<your bot secret token>/getUpdates
  5. In order for bot able to send message to the group, you must add bot to the group
  6. In order for bot able to send message to a person, the person must send a message to the bot first

source

Send message to telegram via API

We can send message to a group or channel via API

What we need is token and chat_id. Bot and bot token to get from @botfather. While chat_id to get from telegram group/channel. i.e. @group_name

$token = "YOUR_BOT's_TOKEN";

$data = [
    'text' => 'your message here',
    'chat_id' => 'the_chat_id_here'
];

file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) );

Learning Vue

Some notes taken while learning vue.

to start using vue can include this file.

<script src=”https://unpkg.com/vue”></script>

Vue object

  • el: “#id – define the element
  • data – define data
  • methods – define method/functions

Vue Directives

  • v-bind – can bind to any attribute in element. e.g. class, title, type, style, href
    • v-bind is use to control the html element attribute where “moustache {{ }}” cannot be used
    • example <div v-bind:class=“{ active: isActive }”></div>
  • v-if=”status”
  • v-for=“item in items” – normally use with <li> elements
  • v-on – example <button v-on:click=”callAFunction”>click me</button>
  • v-model (2 way data binding). example <input v-model=“message”>. message can be displayed as {{ message }} and can be data inside vue script
  • guide/tutorial on directive
  • full list of directives

Vue Components

Vue.component (‘name’, {

})

  • first argument – name of the component
  • props
  • template

API reference

vue cheat sheet

 

Auto-conversion word to PDF on server side

There is a requirement for a system to convert a ms word file to PDF on server side.

References

  1. using libreoffice
  2. using python and openoffice

direct command

libreoffice --convert-to pdf /path/to/file.{doc,docx}
 OR
shell_exec('/usr/bin/libreoffice --headless --convert-to pdf:writer_pdf_Export --outdir /var/www/html/demo/public_html/src/var/output /var/www/html/demo/public_html/src/var/source/sample.doc');

OR

libreoffice --headless -convert-to pdf fileToConvert.docx -outdir output/path/for/pdf

above sample codes taken from different sources. Actually a similar command with some different optional parameters.

PHP array tips

Some tips.

1. Remove duplicates in array using array_unique

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

2. Remove empty elements use array_filter

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

3. value is in the array use in_array

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

4. key exist in array use array_key_exist

bool array_key_exists ( mixed $key , array $array )

 

Web application security

Below are some security checklist for a web application.

  1. Login screen
    1. to avoid brute force attack, display captcha after 3-5 attempts from same IP
    2. enforce longer password more than 8
  2. Registration (or open form)
    1. display captcha for all form to submit
  3. SSL is a must. free or paid
  4. displaying primary id for table in URL must be avoided, use other ID instead. E.g. a secure id consisting of unique 10 randomized alphanumeric