Cron Job重命名网页

时间:2014-11-14 04:38:38

标签: php cron jobs

首先,我要说我对Cron Jobs或PHP文件一无所知,所以如果你能够回复,请记住我的新手状态 - (使用小词并慢慢说!)

我是我们组织网站的网站管理员,我正在努力做一些简单但耗时的事情:在我们的网站上访问主网页,并在感恩节和圣诞假期自动更新。我已经创建了我需要的特定网页,并将它们上传到GoDaddy服务器,但我真的没有时间进入并随着时间的推移手动重命名页面;我告诉Cron Job只是用来自动保存现有页面并重命名新页面。

具体来说我想做什么: 1)位于根目录中的文件Index.htm可以保存或重命名,以便在假期结束后我可以返回它。 2)现在名为/ Holiday Pages / happy_thanksgiving.htm的文件将于11月25日移至根目录并重命名为index.htm与12月24日名为merry_christmas.htm的文件相同。 3)GoDaddy有一个Cron Job控制面板,允许我在特定时间某一天运行特定的脚本,所以我不认为日期代码需要嵌入到脚本本身 - 但是我&# 39;不知道在这个脚本中放什么 - 通过与GoDaddy的人交谈,他们建议使用PHP脚本。 4)在这个PHP脚本中,我需要编写什么命令(具体示例请 - 一个示例脚本将是非常棒的,非常感谢! 5)这个脚本的扩展需要什么? .TXT或.PHP ??

提前致谢!再一次,请记住我在这里 - 并原谅我的无知!

----------------------------------------------- -------- UPDATE 111/15/14 ------------------------------------ -------------------- 以下是我到目前为止所尝试的内容,使用了一些建议 - 数字1)2)等是试用脚本编号,然后由GoDaddy的Cron作业管理员调用。

1)--------------------------------------------- -

<?php
$target = "/holiday_pages/happy_thanksgiving.html"; 
$newName = "/holiday_pages/index.htm";
$renameResult = rename($target, $newName);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $target . " is now named " . $newName;
} else {
 echo "Could not rename that file";
}
?>

2)--------------------------------------------- ---

rename('/holiday_pages/happy_thanksgiving.html', '/holiday_pages/index.htm');

3)--------------------------------------------- ---     

rename("/holiday_pages/happy_thanksgiving.html", "/home/user/password/holiday_pages/index.htm");

?> 

4)--------------------------------------------- ---

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('./HTML/holiday_pages/index.htm','index.htm.bak');
copy('./HTML/holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure      if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

}

5)--------------------------------------------- -----

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('../HTML/holiday_pages/index.htm','index.htm.bak');
copy('../HTML/holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

}

6)--------------------------------------------- -----

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('/HTML/holiday_pages/index.htm','index.htm.bak');
copy('/HTML/holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure if     this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

}

7)--------------------------------------------- ------

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.15') {
copy('./holiday_pages/index.htm','index.htm.bak');
copy('./holiday_pages/happy_thanksgiving.html','index.htm'); // you need to make sure if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
copy('index.htm.bak','index.htm');

4 个答案:

答案 0 :(得分:0)

只需创建一个文件并为其命名,例如holiday_copy.php。这是脚本本身:

<?php

$date = new DateTime();
$date -> format('Y.m.d');

if ($date == '2014.11.25') {
    copy('index.htm','index.htm.bak');
    copy('../Holiday Pages/happy_thanksgiving.htm','index.htm'); // you need to make sure if this (../Holiday Pages/) is the right path to your file!

}
else if ($date == '2014.11.26') {
    copy('index.htm.bak','index.htm');
}
else if ($date == '2014.12.24') {
    copy('index.htm','index.htm.bak');
    copy('../Holiday Pages/merry_christmas.htm','index.htm'); // again, you need to make sure if this (../Holiday Pages/) is the right path to your file!
}
else if ($date == '2014.12.27') {
    copy('index.htm.bak','index.htm');
}

&GT;

你必须在所需的日子里运行这个脚本,最好是在

0:01 on eg 2014-12-24

您需要将此文件放在index.htm所在的根目录中。如果您不想这样做,请在此脚本中添加适当的路径到所有复制的文件 - 例如:

copy('index.htm','index.htm.bak');

确保有权访问所有目录和文件!记得在假期后的几天打电话给它,以便让你的旧index.htm回来。

答案 1 :(得分:0)

更好的解决方案是根据日期创建文件和代码动态包含文件,示例代码如下:

$date = date("m-d-y", time());  // current date

$holidays = array('12-25-14' => 'christmas-page.php', '11-27-14' => 'thanksgiving-day-page.php'); // create all holidays here

$page = isset($holidays[$date]) ? $holidays[$date] : 'default-index.php';

include($page);

简单的逻辑,将正常工作

答案 2 :(得分:0)

这不是PHP解决方案。这是一个SSH /命令行解决方案,也不是Cron Job。大概需要2-5分钟。

假设您使用的是Linux主机方案,并且已为您的帐户启用了SSH(远程登录);如果没有,您可以要求/致电GoDaddy为您的帐户启用此功能。

进入命令行后,请执行以下操作。

安全第一,所以备份我们要移动或乱七八糟的所有文件:

  1. cd PUBLIC_ROOT_DIR
  2. cp“index.htm”“index.htm.bak”
  3. cp“假日页面/ happy_thanksgiving.htm”“假日页面/ happy_thanksgiving.htm.bak”
  4. cp“Holiday Pages / merry_christmas.htm”“假日页面/ merry_christmas.htm.bak”
  5. 感恩节之前:

    1. cd PUBLIC_ROOT_DIR
    2. mv“index.htm”“index.htm.off”
    3. mv“Holiday Pages / happy_thanksgiving.htm”“index.htm”
    4. 感恩节后:

      1. cd PUBLIC_ROOT_DIR
      2. mv“index.htm”“happy_thanksgiving.htm.off”
      3. mv“index.htm.off”“index.htm”
      4. 圣诞节前:

        1. cd PUBLIC_ROOT_DIR
        2. mv“index.htm”“index.htm.off”
        3. mv“Holiday Pages / merry_christmas.htm”“index.htm”
        4. 圣诞节后:

          1. cd PUBLIC_ROOT_DIR
          2. mv“index.htm”“merry_christmas.htm.off”
          3. mv“index.htm.off”“index.htm”

答案 3 :(得分:0)

正如你们许多人提到的,CRON JOB不是一个好选择;而是一个重定向脚本,它根据服务器日期将Web用户带到正确的页面。我得到了一位以编程为生的朋友的大力帮助 - 虽然这篇文章已有一年多的历史了,但我想我会分享最终结果和工作脚本,以了解其他人可能拥有的机会。同样的问题。请注意,此脚本安装在Web服务器的根目录中,并命名为“index.html”。还创建了一个用于非假日使用的网页,称为“main.html”,如果不满足条件列表,脚本将使用此页面。

这个解决方案对我们非常有用,我非常感谢所有的回复!

 <!DOCTYPE html>
<!--
-->
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

    <script type="text/javascript">
        var today = new Date();

        var newyear1 = new Date("2016-12-30");
        var newyear2 = new Date("2016-01-05");

    var valentines1 = new Date("2016-02-14");
        var valentines2 = new Date("2016-02-16");

    var spring_forward1 = new Date("2016-03-07");
        var spring_forward2 = new Date("2016-03-16");

        var easter1 = new Date("2016-03-20");
        var easter2 = new Date("2016-03-29");

    var easter3 = new Date("2016-03-28");
        var easter4 = new Date("2016-03-30");

    var mothers_day1 = new Date("2016-05-07");
        var mothers_day2 = new Date("2016-05-11");

    var memorial_day1 = new Date("2016-05-27");
        var memorial_day2 = new Date("2016-06-02");

    var fathers_day1 = new Date("2016-06-18");
        var fathers_day2 = new Date("2016-06-21");

    var july_4th1 = new Date("2016-07-04");
        var july_4th2 = new Date("2016-07-06");

    var laborday1 = new Date("2016-09-04");
        var laborday2 = new Date("2016-09-09");

    var election1 = new Date("2016-11-07");
        var election2 = new Date("2016-11-10");

    var thanksgiving1 = new Date("2016-11-20");
        var thanksgiving2 = new Date("2016-11-30");

    var xmas1 = new Date("2016-12-10");
        var xmas2 = new Date("2016-12-30");

        // default index page. if no date ranges match, this page will     be     used.
        var pageName = "main.html";


        // new_year 2016
        if(today >= newyear1 && today <= newyear2)
        {
            pageName = "/holiday_pages/happy_new_year.html";
        }

        // Valentines 2016
        if(today >= valentines1 && today <= valentines2)
        {
            pageName = "/holiday_pages/valentines.html";
        }

        // time_change_forward 2016
        if(today >= spring_forward1 && today <= spring_forward2)
        {
            pageName = "/holiday_pages/spring_forward.html";
        }

        // Easter General 2016
        if(today >= easter1 && today <= easter2)
        {
            pageName = "/holiday_pages/easter_1.html";
        }

        // Easter Resurrection 2016
        if(today >= easter3 && today <= easter4)
        {
            pageName = "/holiday_pages/easter_2.html";
        }

        // Mothers Day 2016
        if(today >= mothers_day1 && today <= mothers_day2)
        {
            pageName = "/holiday_pages/mothers_day.html";
        }

        // Memorial Day 2016
        if(today >= memorial_day1 && today <= memorial_day2)
        {
            pageName = "/holiday_pages/memorial_day.html";
        }

         // Fathers Day 2016
        if(today >= fathers_day1 && today <= fathers_day2)
        {
            pageName = "/holiday_pages/fathers_day.html";
        }

        // July 4th 2016
        if(today >= easter3 && today <= july_4th)
        {
            pageName = "/holiday_pages/july_4th.html";
        }

        // labor_day 2016
        if(today >= laborday1 && today <= laborday2)
        {
            pageName = "/holiday_pages/labor_day.html";
        }

        // Election Day 2016
        if(today >= election1 && today <= election2)
        {
            pageName = "/holiday_pages/election_day.html";
        }

        // Thanksgiving 2016
        if(today >= thanksgiving1 && today <= thanksgiving2)
        {
            pageName = "/holiday_pages/happy_thanksgiving.html";
        }

        // Christmas 2016
        if(today >= xmas1 && today <= xmas2)
        {
            pageName = "/holiday_pages/merry_christmas.html";
        }



        // redirect to pageName
        document.location.href = pageName;

    </script>
 </body>
 </html>