如何在PHP中随机化背景图像?

时间:2015-01-07 22:19:11

标签: php css

我已经关注this tutorial,但它不起作用,这是我的剧本:

<?php
        function ceklogin(){
            session_start();
            if ($_SESSION['loggedin'] != 1) {
                header("Location: login.php");
                exit;
            }}
        function css(){

          $bg = array('images/angel-beats1.jpg', 'images/angel-beats2.jpg', 'images/angel-beats3.jpg'); // array of filenames

          $i = rand(0, count($bg)-1); // generate random number size of the array
          $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen

        echo '
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>OpenWrt Angel Beats Edition (v1.0)</title>
        <style type="text/css">
        body {
            margin-left: 0px;
            margin-right: 0px;
            background: url(images/<?php echo $selectedBg; ?>) no-repeat;
            background-size:cover;
             }
        </style>
        </head>
        <body> bla bla bla
        </body>
        </html>';
    ?>

我哪里出错了?我的图片位于名为&#39; images&#39; stackoverflow一直告诉我要添加更多细节,但我认为这是我能给予的全部。

3 个答案:

答案 0 :(得分:2)

shuffle($bg);

然后echo $bg[0];

您正在使用复杂的代码来轻松完成工作。

答案 1 :(得分:0)

你不应该让php回复更多的PHP代码。你应该这样做:

    echo '
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>OpenWrt Angel Beats Edition (v1.0)</title>
    <style type="text/css">
    body {
        margin-left: 0px;
        margin-right: 0px;
        background: url(images/';

    echo $selectedBg;
    echo ') no-repeat;
        background-size:cover;
         }
    </style>
    </head>
    <body> bla bla bla
    </body>
    </html>';

答案 2 :(得分:0)

已更新

@hillz试试这段代码。

我认为你没有调用css函数,你错过了声明中函数css的结束括号。

    <?php
        function ceklogin(){
            session_start();
            if ($_SESSION['loggedin'] != 1) {
                header("Location: login.php");
                exit;
            }
        }

        function css(){

          $bg = array('images/angel-beats1.jpg', 'images/angel-beats2.jpg', 'images/angel-beats3.jpg'); // array of filenames

          $i = rand(0, count($bg)-1); // generate random number size of the array
          $selectedBg = "$bg[$i]"; // set variable equal to which random filename was chosen

          return $selectedBg;
        } 

        $bgUrl = css(); 
?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>OpenWrt Angel Beats Edition (v1.0)</title>
        <style type="text/css">
        body {
            margin-left: 0px;
            margin-right: 0px;
            background: url("<?php echo $bgUrl; ?>") no-repeat;
            background-size:cover;
             }
        </style>
        </head>
        <body> bla bla bla
        </body>
        </html>';