Day 4, working on mvp

Contents

  • DbLess Email Verification
  • Crontab setup
  • Stub for sendmail functionality

DbLess Email Verification

So, how to verify and store email without actually using any database, you need a way to securely store the sent verification code and also verify the user input code.

We have to store the code on the client side in the cookie securely, but cookies are not secure and can not be used to store sensitive info. My solution for this is to

  • Hash the code, and store only the hash in the cookie
  • Get code from user in a form, hash it using the same algorithm and only compare the hashes for match.

This approach will make it secure to store verification code on the client machine

Note : This is not the best solution possible, We can also send user an link to the verification. (will be testing this method also)

function storeCodeHash($code){
     $codeHash = hash('sha256' , $code);
     setcookie('codeHash', 
                $codeHash , 
                time()+60*2);
    }

I have set the cookie to expire after 2 minutes.


Crontab setup

crontab is a command line utility in unix systems allowing users to schedule jobs to be run at specific time.

How to add a cron job in your local system?

To add a cron job, we can to use crontab

  1. crontab -e : this will open a file in the vim editor to add your new cron job
  2. The format for adding fron job is as shown below
*    *    *    *    *   /home/user/bin/somecommand.sh
|    |    |    |    |            |
|    |    |    |    |    Command or Script to execute
|    |    |    |    |
|    |    |    | Day of week(0-6 | Sun-Sat)
|    |    |    |
|    |    |  Month(1-12)
|    |    |
|    |  Day of Month(1-31)
|    |
|   Hour(0-23)
|
Min(0-59)

copied from [cronitor.io]
  1. If we want to schedule a job to be run every 5 minutes we can use following expression */5 * * * * run.php

‘*/n’ is used to add step value so it can run every n interval. I used crontab.guru to learn more about the time expression and how to use it.

  1. press i to go into insert mode in vim.
  2. type your cronjob */5 * * * * run.php
  3. press escape to get out of vim
  4. type :wq to write and quit vim
  5. It will give output as crontab: installing new crontab in the terminal.

Ok, we now know how to schedule a cron job but how to make it so that the send updates functionality on our server is being called properly?

  • I tried first run the script using php script.php from the terminal, it was running without error but the mailghog was not capturing the mail.
  • As we are running this php script standalone and not on the server, that’s probably why the mailhog couldn’t capture the sent mails.

the curl utility : it is used to send http requests to webservers from command line, that’s exactly what I wanted.

  • I updated the cron job with curl and got it to capture mails in mailhog.

Stub for SendMail functionality

As we want the sent mail to look better, I am sending html mail, we can format the mail how we want to and also make it easy for the user to understand it better

Sending HTML mail from php

  • I am using inbuild mail() function to send mail to the user
  • We have to set appropriate headers in order to be able to send a HTML email
 $headers[] = 'MIME-Version: 1.0';
 $headers[] = 'Content-type: text/html';

  mail($email , $subject , 
       $message , implode("\r\n" , $headers))

implode is used to combine the array elements in a string using a some expression


XML Parsing is yet to be done which is a critical thing for this project

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *