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


How to randomize background images in PHP?

我已经按照本教程进行操作,但它不起作用,这是我的脚本:

<?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>';
    ?>

我哪里做错了?我的图像在一个名为"images"的文件夹中 stackoverflow 不断告诉我添加更多详细信息,但我认为这就是我所能提供的。

shuffle($bg);

然后echo $bg[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>';

已更新

@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>';