How to make token valid as per user using PHP and MySQL - php

I need one help. I am doing the token based login Webservice using PHP and MySQL. Here I am setting the time duration, If no activities are happened within that time interval the code should return the failed status. I am explaining my table below.
db_user:
id user_id token added_time
1 2 aqs234reftgyh 31-01-2018 12:23 PM
The above is my table with the required token and date-time for user_id=2. Here my need is each time of user action the input will be user_id=2 and token=aqs234reftgyh pass from user end to match with database. If the current user action time is more than 15 min of token added time then the code will return as false otherwise true.

calculate the time difference and if it exceeds 15 minutes then return false
$endTime = strtotime("+15 minutes", strtotime($added_date));
$currentTime = strtotime(date("d-m-Y H:i:s p"));
if(round(abs($currentTime - $endTime) / 60,2) > 15){
return false;
}

Related

How to generate url from API which data is chaning every hour PHP

I have API which changing its data on every hour. Example output:
{"status":true,"hash":"yJscAtmUB0IXz1SZ","link":"https://url.com/video.m3u8","thumbnail":""}
Actually just link is changing on every hour. That link I'am using to stream video via player.js. For example:
mysite.com/watchvideo.php?url=**link-by-api**
I cant figure out how to get the new link which is changing.
Solution 1 :
write a php code in your project that checks if it was more that 1 hours , get the new link .
<?php
$dateTime = strtotime('now'); // Get current time in seconds
$difference = $dateTime - $updatedTime; // Get Difference Between Right Now And Updated Time
// $updatedTime is the last time api link is updated
// you should store the last updateDate in somewhere ...
if ($difference >= 3600) { // 3600 seconds = 1 hour
// Update the api link
}
Solution 2 :
you can use cron job to run this php file every 1 hour and store the api-link into the database

What's the best way to count unique visitors on my webpage?

I create a php class to count unique visitors on my webpage, my ideas is create a cookie that stays on the visitor computer until 23:59 of the current day, if the cookie exite it means that the site has already been visited and does not do anything, if it does not exist then create the cookie and account new visit
this is the code I have for creating the code, if the cookie exite it return false, if not create the cookie and return true.
**
function CriakCookieDia(){
if (!isset($_COOKIE[sha1('visita')])){
$tempoAteAoFimDoDia=strtotime('tomorrow') - time();
setcookie(sha1('visita'), true, time()+$tempoAteAoFimDoDia);
return true;
}else{
return false;
}
}
**
but I'm not having the expected result, instead of unique visits, it's like I count the number of views on the site, it's like I'm not creating the cookie, what's wrong about my approach?
The expiry time needs to be a timestamp for a specific date/time.
This code give a date in 1970.
strtotime('tomorrow') - time(); // 01/01/1970 11:15:01
So your code was just a bit too complicated, set the cookie expiry time to strtotime('tomorrow') which will give tomorrows date at 00:00:00 i.e. the beginining of tomorrow
This code, run at 28/06/2018 13:40:00 will give:
echo date('d/m/Y H:i:s',strtotime('tomorrow')); // 29/06/2018 00:00:00
Try this instead
function CriakCookieDia(){
if (!isset($_COOKIE[sha1('visita')])){
setcookie(sha1('visita'), true, strtotime('tomorrow'));
return true;
}else{
return false;
}
}

Limit the access of a function to once every 24 hours PHP + MySQL

I would like to limit the access of a function i've created to once every 24 hour based on the users IP address. I would also like the PHP script to delete the MySQL record if it's older than 24 hours.
If the user already has used the function within 24 hours, show them a message and prevent the script from continue running.
If the user already has used the function but 24 hours has passed since he used the function, delete the MySQL record and let the script continue running.
I'm lost and as you can see i am also missing some statements for deleting old records (-24 hours)..
Could anyone provide me with an example of how to do this? Thanks
Get client's IP address and store it with current date and time if the record doesn't exist.
Fetch the record and add 24 hours to its date and time value and check it with the current date and time every time the script is executed.
You need if else conditional statements to check if the 24 hours time is over or not. Based on that, you will control the execution of the function you want to.
I think I don't want to write much of theory. Here, I've written the pattern what the code looks like:
if(!$record_in_db) {
// create record with the client's ip address and the current date and time
// invoke the function you want - This is the code to trigger the function first time for the new IP address
} else {
// fetch_record_from_db
// add 24 hours to its date and time value
// check it with current date and time
$record_date_time = new DateTime('22-12-2016 22:45:20'); // this value should be fetched from database
$record_date_time_by_24_hours = $record_date_time->modify('+1 day');
$current_date_time = new DateTime(date('d-m-Y H:i:s', strtotime('now')));
$date_time_diff = $current_date_time->diff($record_date_time_by_24_hours);
if($date_time_diff->invert == 0) {
// Do something
} else {
// update the date and time of the record to NOW which is current date and time
// invoke the function you want
}
}
I can't write you the whole code. I could only give you some hints. You need to build the code from it. Hope I've given you right hints that could help you.

Expire unix or time() code after x hours

I am trying to create a forgot password feature which will expire the link created after x hours. So I am storing time() data in database value when a user requests a password reset. So how can I expire it?
three options:
compare the time you saved on the db with the one you get when the user click the link
Use a cron job and make it run periodically
Just don't save in the db and make the link to care about everything. You could use a signature + a salt to avoid users to modify this link
like:
$now = time();
$sk = sh1($user_id . $now . "yoursupersalthere")
$link = "http://www.example.com/forgot.php?id={$user_id}&ts={$now}&sk={$sk}"
that will be the link you sent to the user. Then to make the check
$ts = $_GET['ts'];
$user = $_GET['id'];
$sk = $_GET['sk'];
if (!$sk == sh1($user_id . $now . "yoursupersalthere")) {
die("bad signature");
}
elseif (time() - $ts > 3600 /* or put your expiration limit */) {
die('link expired');
}
// do your job
You're probably having a table entry with a reset link, just add a date field to it, and then either include a WHERE expiredate<NOW() or clean the table from time to time with a simple DELETE from table WHERE expiredata<NOW().
One method of doing this is to check to see if the link is expired when the link is clicked -- some pseudo-code:
// when the link is clicked pull the information from the database and get the time
// SQL goes here
// this will give you the difference in seconds
$diff = time() - $timestamp_from_db;
// we'll pretend the time expires in 8 hours
$expires_in = 8 * 60 * 60;
// for this example we'll pretend the expiration is 8 hours
if($diff <= $expires_in)
{
// has not been more then 8 hours
}
else
{
// has been more then 8 hours
}
The best way to do this that keeps the table clean is to implement the following:
The table needs at least the account ID with a UNIQUE index and foreign key to the accounts table, the hash with a UNIQUE index and a timestamp.
In the page that creates the link, do not allow "reset my password" based on information that can be obtained by a random person. If you do this, one can fill your table with reset password requests and generate spam and security concerns with your users.
In the page where the link is verified first delete all expired records by comparing NOW() with the stored timestamp, then simply SELECT using WHERE='$hash' (of course, you sanitize $hash). Given the UNIQUE index on the hash, this can only return one row or no rows.
The UNIQUE index on the account ID ensures that people cannot request multiple resets within the expiration time.

PHP compare time with undefined Timezones

I need to compare times, I am using variables to define timezones, the current time and the time to be compared.
I am using this script to do certain actions based on what the user selects. The user can select a time when this action should be done, a date and a timezone.
If the current time is > or equal than the time selected by the user those action should run.
So I need to compare the current time and the time selected by the user. The current time is given by the timezone selected by the user.
This is the code I have. The comparison is not working fine. It thinks the oppposite thing than what should actually thinks... I need an help.. possibly not using php 5.3 or above. Better if functions are avaialbe from version 4 to 5
date_default_timezone_set($TimeZone); //all the possible php timezones available can be choosen by the user
$todaydate = date('Y-m-d');
$time_now=mktime(date('G'),date('i'),date('s'));
$NowisTime=date('G:i:s',$time_now); //the time given by the selected timezone above
$time = $ris['Time']; //the time defined by the user in the DB
if($NowisTime >= $time){
DO THE ACTIONS
}else{
exit;
}
If user defined time in database is relative to same timezone. you could try this snippet follow:
$dateTimeZone = new DateTimeZone("Asia/Chongqing");
$localTime = new DateTime("now", $dateTimeZone);
$definedTime = new DateTime("2011-05-29 10:00:00", $dateTimeZone);
if ($localTime >= $definedTime) {
echo "It is about time.";
} else {
// do nothing.
}
It would be better to save user defined time with Unix Time Stamp.

Categories