1

Is there a way to add some code in PHP to make the file expire after some days that I specified?

For example, I designed a PHP script for a client and this script is valid for 365 days and I want the script to expire after 365 days and show a custom message.

Of course I will be encoding the PHP file.

is there any code that I can into a single PHP without additional files to add expiry date on the file?

Thank you

Arta Seyzed
  • 37
  • 1
  • 7
  • Use [getdate()](http://www.php.net/manual/en/function.getdate.php) in an `if` statement at the start of the file? If the current date is 365 days after the start date (or more) then return "Out of date message" else do rest of script? – Kvothe Sep 06 '15 at 17:05
  • I would be interested to know exactly why you want to do this? – Kvothe Sep 06 '15 at 17:06
  • He maybe trying to make script demos/trials but the client will still be able to remove that function... – Iatrarchy Sep 06 '15 at 17:16

5 Answers5

1

use filectime

$from = strtotime(filectime('yourfile.php'));// for linux use filemtime
$today = time();
$diff= $today - $from;
if(floor($diff/ (60 * 60 * 24)) > 365)
{
  echo "File expired (365 days)";
}
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35
0

The PHP way of doing it is to just put a check like this at the top of the script:

$expired = (time() > strtotime('2016-09-06'));
if ($expired) {
  // Do something, like output an error
  die();
}

You can go more fancy on it by checking validity via a remote call to your own server, but in the end I would say your bigger issue is that this check easily can be removed by your client if he has the source code. You say you will "encode" (encrypt?) the script, but I am not aware of any real ways to do that in PHP as it is a script language (the best you will likely be able to do is obfuscate it).

Erik Johansson
  • 1,646
  • 1
  • 14
  • 19
  • This one is not working for me. Of course I will be encrypting the entire PHP code after this modification. – Arta Seyzed Sep 07 '15 at 23:08
  • Actually this works. I was on cashe. prefect solution and it does exactly what I wanted. – Arta Seyzed Sep 07 '15 at 23:20
  • Glad I could help. This is where you mark my comment as the answer as well though ;) Noticed you haven't done that on your other questions either. – Erik Johansson Sep 08 '15 at 13:11
  • Goodie. Just did. new to stackoverflow. Its an amazing source and I never thought that I would get this many responses this quick. Cheers mate – Arta Seyzed Sep 08 '15 at 17:50
0

An extremely easy way would be to define a constant as the current date to begin counting down, store it is as a Unix TIMESTAMP, in my opinion. Then use time() at the top of the PHP script and do an if statement like:

if ((time() - 31536000) > START_TIME)) {
    //31536000 seconds in 365 days
    throw new TimePassedException;
}

Or else, you can use date() or strtotime to format your choosing for storing the times/dates.

q.Then
  • 2,743
  • 1
  • 21
  • 31
0

First thing you would want to do is find the number of days elapsed. That part has been answered here Date elapsed in php Now if the output starts to come as positive means that you can start showing a custom message. For that you can use

die("custom message here");

die will also stop the execution of any script after that point

Community
  • 1
  • 1
georoot
  • 3,557
  • 1
  • 30
  • 59
0
$Today = date('y:m:d');

                    $connect = mysqli_connect("localhost", "root", "root", "kiotchamber");
                    $query = "SELECT * FROM  instantnews where expired_date >='$Today' ORDER BY id DESC LIMIT 0,5";
                    $result = mysqli_query($connect, $query);
                    ?>
Robert
  • 5,278
  • 43
  • 65
  • 115