Make Link expire after X minutes - php

Okay I am sending an activation email link to user's email. My Activation link is working perfect I wants to make that link expire after X minutes.
Here is my code:
$base_url='http://172.16.0.60/WebServices/';
$activation=md5($email.time());
email format to HTML
$mail->Subject = 'Activate Your Account';
$mail->Body = 'Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.'';
Now my
.htaccess file
RewriteEngine On
RewriteRule ^activation/([a-zA-Z0-9_-]+)$ activation.php?code=$1
RewriteRule ^activation/([a-zA-Z0-9_-]+)/$ activation.php?code=$1
Now how can I make my link expire? I dont have timestamp field in my database.

I dont have timestamp field in my database.
Then add one.
Add a field to record when the link was generated and you can then instantly check against that time when they visit. There is no need to waste time trying to think of hacks when you can simply adjust your setup accordingly.

I got my answer using this simply,
Send a timestamp in link and on other end while accepting the link I have to check the time with current time stamp.
$base_url='http://172.16.0.60/WebServices/';
$activation=md5($email.time());
$time = time();
email format to HTML
$mail->Subject = 'Activate Your Account';
$mail->Body = 'Hi, <br/> <br/> We need to make sure you are human. Please verify your email and get started using your Website account. <br/> <br/> '.$base_url.'activation/'.$activation.$time.'';
While when user clicks on link, following code will check the expiration of link.
$u_time = $this->input->get('time'); // fetching time variable from URL
$u_time = md5($u_time);
$cur_time = time(); //fetching current time to check with GET variable's time
if ($cur_time - $u_time < 10800)
{
// link is not expired
}
else
{
// link has been expired
}

date_default_timezone_set("Asia/Kolkata"); // set time_zone according to your location
$expire_date = "2019-03-26 14:45:00"; // time that you want the link to expire
echo $now = date("Y-m-d H:i:s"); // your current time.
if ($now>$expire_date) {
echo " Your link is expired";
}
else
{
echo " link is still alive";
}
you can find your time_zone here
Hope this helps:)

Related

PHP: If $date is today, send an email - but only send it once

I'm trying to add something to my website where:
If the date is a certain date, I want it to send me an email; however every time I refresh the page on that day it resends the email. I don't want it to do this - only send the email once.
I know a little about SESSIONS and such but not enough to implement them. This is my code, any help would be appreciated.
TL;DR - How to send an email from the website once if the date is a set date?
//Inspections
<?php
$recipients = array("Fake#email.com","Another#FakeEmail.com");
$to = implode(",", $recipients);
$subject = "Inspection Reminder.";
$bedMsg = "There is a BEDROOM inspection in five days!";
$comMsg = "There is a KITCHEN inspection in five days!";
$dateInspect = date("05/11/16"); //Set to a certain date for testing
if ($dateInspect == "05/11/16"){
mail($to,$subject,$comMsg);
} elseif ($dateInspect == "26/11/16"){
mail($to,$subject,$comMsg);
} elseif ($dateInspect == "24/11/16"){
mail($to,$subject,$bedMsg);
} else {
;
}
?>
The better solution is to use an database to store the flag of sending the email on a specific day.
But, if you running a simple website and don't want to go for database, you can simply create a file after the sent, and always check if that file exists.
$dateInspect = date("05-11-16");
if(!file_exists("mail_sent_".$dateInspect.".txt")){
// .. SEND YOUR E-MAIL
// create the file
file_put_contents("mail_sent_".$dateInspect.".txt", "mail sent this day");
}
Attention: Don't use slashes on date, as it will generate an filename as: mail_sent_05/11/16.txt which is an invalid name, filenames can't have slashes.

expiring php redirect on set date

I.m looking for a way in Php to have a Redirect expire on a said day.
The use for this is that I'm sharing file on a sever using the code below.
What I like is to have that redirect take them to a page that lets them know there time is up and they can see the Redirect anymore or tell I update the date of expire.
if (test){
---do something---
die("<script>location.href = 'http://file.here.com'</script>");
} else {
return false;
}
I have also try
function BeforeProcessList(&$conn, &$pageObject)
{
$target_date = new DateTime("05-01-2014");
$today = new DateTime();
if((int)$target_date->diff($today)->format('%r%a') >= 0)
{
header("Location: testview_list.php");
exit();
}
}
There are several scenarios for this case but i suggest to use session.
Use additional session to be taken once your privilege period is valid and make sure that you've already insert a date when a user get into that file along with value that indicate this user is allowed , for example,1 so you can change a value to ,for example,0 after specific period and that user will be denied automatically or redirected to another page
I got it to work.
<?php
// has this expired?
$expire_date = "2015-08-25"; // good until Aug 8/15
$now = date("Y-m-d");
if ($now>$expire_date) {
header("Location: http://yoursite.com/outoftime.html");
die();
}
// and now the unexpired part of the page ...
header("Location: http://yoursite.com/fileshare/");
die();
Thanks for the help guys

Making the variable unset after two minutes

Hey guys am writing a small validation script which uses a simple token to login from the input..When the token is right the user must succesfully login and after two minutes the token must expire and give user a message token expired..But here when i use the token it also came with the message token destroyed ..i want to use the token for 2 minutes and i want the token to be expired in 2 minutes.
I have the html file
<form action="gethints.php" method="post">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
Php file
<?php
$name = $_POST['fname'];
$currenttime = time();
$token = 'sample';
$timetounset = strtotime("2 minutes");
if($name != $token) {
echo 'you cant login';
} else {
echo 'you have succesfully logged in <br>';
}
if($currenttime > time() - $timetounset) {
unset($token);
echo "you cant use this token anymore";
} else {
echo 'token is not destroyed';
}
When i run this code and type sample in the input box i get the message like
you have succesfully logged in
you cant use this token anymore
What i need is when i type the id as sample i want to get the message you have succesfully logged in and after two minute when i use the same id i need to get the message like you cant use this token anymore
Thanks for your help..
Could use a session here:
first, store the last time the user made a request
<?php
$_SESSION['timeout'] = time();
?>
in subsequent request, check how long ago they made their previous request
<?php
if ($_SESSION['timeout'] + 2 * 60 < time()) {
// session timed out
} else {
// session ok
}
?>
EDIT:
Don't forget to have session_start(); on top of your code && check if $_SESSION['timeout'] exists, above code is just an example.
The most simple way to approach this imo is to set a cookie with an expiration of 2 minutes.
Store a variable or flag in the cookie and check the contents each time you want to do a validation.
setcookie("TestCookie", $value, time()+120);
Read out the value:
if (isset($_COOKIE['TestCookie'])) {
// Good cookie
} else {
// expired or invalid
}
For information on how to set a cookie and expiration:
http://php.net/manual/en/function.setcookie.php
People can play around with cookies. I suggest you create a database table tokens with token and expires columns. When you insert the token add this to your sql. ...... Insert ....... Timestampadd(now(), interval minute 2); To validate just check if now() is less than expires.

Emailing php output

I have built a jquery mobile form that submits to a php file. I want the following php output to also have the option of emailing it to the user.
<?php
$DCity=$_GET["DCity"];
$ACity=$_GET["ACity"];
date_default_timezone_set($DCity);
$DZone = date("Y-m-d h:i:s A");
date_default_timezone_set($ACity);
$AZone = date("Y-m-d h:i:s A");
echo ((strtotime($DCity) - strtotime($ACity))/3600)." hour Timezone Difference</br>";
?>
This will output something like "5 hour Timezone Difference".
Below this php I have a this code which should save the output for an email.
<?php
ob_start();
$DCity=$_GET["DCity"];
$ACity=$_GET["ACity"];
date_default_timezone_set($DCity);
$DZone = date("Y-m-d h:i:s A");
date_default_timezone_set($ACity);
$AZone = date("Y-m-d h:i:s A");
echo ((strtotime($DCity) - strtotime($ACity))/3600)." hour Timezone Difference</br>";
$var=ob_get_flush();
?>
The user then has the option to email this to themselves. If they click the link it takes them to a new form to enter in their email address which posts to a new php that contains this code.
<?php
$to = $_GET["email"];
$subject = "Your Personalized JetLag Pilot Plan";
require 'OriginalOutput.php';
$body = $var;
mail($to,$subject,$body);
echo "Mail sent to $to";
?>
The original php output is working correctly. However whenever I send an email that should contain the same output, it always says "0 hour Timezone Difference." The email form is working correctly just not carrying the OriginalOutput.php. Anyone know where my mistake is?

how to disable a button for a specific time in php

I have an edit button for users to edit their posted jobs. I want that button to disable or disappear a week after published day.how can I do this in php. thanks advance...
<?php
$timePublished = 1334301250; // unix time when job posted
$afterOneWeek = $timePublished+(60*60*24*7); // 1 week
$dateNow = date('U'); // unix time for Date Now
if($dateNow > $afterOneWeek){ // if Date Now is greater than our "After One Week"
echo "<button>Button is now visible</button>";
} else {
echo "No button, one week not passed";
}
?>

Categories