如何在 codeigniter 3 中设置背景图像

How to set background-image in codeigniter 3

我几天前开始使用 codeigniter,我无法通过 css file.My 控制器设置背景图像(verifylogin.php)是这样的:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user','',TRUE);
   $this->load->helper('url');
 }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('login_view');
   }
   else
   {
     //Go to private area
     redirect('home', 'refresh');
   }

 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');

   //query the database
   $result = $this->user->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}

而视图(login_view.php)是这个。

<html>
 <head>
   <title>OpediaLab</title>
 </head>
 <body>
   <h1></h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('verifylogin'); ?>

<link rel="stylesheet" type="text/css" href="<?=base_url()?>style.css"/>
<label for="username">Username:</label>
     <input type="text" size="20" id="username" name="username"/>
     <br/>
     <label for="password">Password:</label>
     <input type="password" size="20" id="passowrd" name="password"/>
     <br/>
     <input type="submit" value="Login"/>
   </form>
 </body>
</html>

css 文件只包含这两行:

body{
background-image: url("C:/wamp/www/CI/application/views/immagini/Opedia_LAB.png");
background-color: green;
}

现在,我很确定 css 文件必须在特定的文件夹中(我看过很多教程,但不是都说同样的话)。我最后一次尝试是将文件放在 "views" 中,如图

我希望我说得够清楚,我知道,这是因为我对这个框架还很陌生。

这是我在任何试图改变我所做的事情时所看到的。没有错误,但没有 css 效果。

更新:

这是我从控制台得到的:

像这样更改您的代码::

<html>
 <head>
   <title>OpediaLab</title>
<link rel="stylesheet" type="text/css" href="<?=base_url()?>."application/views/style.css"/>
 </head>
 <body>
   <h1></h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('verifylogin'); ?>


<label for="username">Username:</label>
     <input type="text" size="20" id="username" name="username"/>
     <br/>
     <label for="password">Password:</label>
     <input type="password" size="20" id="passowrd" name="password"/>
     <br/>
     <input type="submit" value="Login"/>
   </form>
 </body>
</html>

试试这个

<html>
 <head>
   <title>OpediaLab</title>
   <link rel="stylesheet" href="<?php echo base_url('style.css');?>">
 </head>
 <body>
   <h1></h1>
   <?php echo validation_errors(); ?>
   <?php echo form_open('verifylogin'); ?>

<label for="username">Username:</label>
     <input type="text" size="20" id="username" name="username"/>
     <br/>
     <label for="password">Password:</label>
     <input type="password" size="20" id="passowrd" name="password"/>
     <br/>
     <input type="submit" value="Login"/>
   </form>
 </body>
</html>

使用 Codeigniter URL 助手 base_url();

我建议您将 css 样式放在与应用程序同级的名为 assets 的文件夹中。

在您的情况下,样式调用应该是:

来自

<link rel="stylesheet" type="text/css" href="<?=base_url()?>style.css"/>

<link rel="stylesheet" type="text/css" href="<?=base_url()?>application/views/style.css"/>

在你的 style.css 变化

body{
background-image: url("C:/wamp/www/CI/application/views/immagini/Opedia_LAB.png");
background-color: green;
}

正文{ 背景图片:url(); 背景色:绿色; }

希望这对您有所帮助..随时提问..如果我有错,请随时发表评论;

请注意我在此处编码;我还没有测试过它..但我已经将它应用到我的项目中..玩得开心!

   body{
         background: url(<?php echo base_url('assets/images/bg_images.jpg');?>);
}
"C:/wamp/www/CI/application/views/immagini/Opedia_LAB.png"

您正在从应用程序目录中获取图像,出于安全原因禁止用户访问,因此您无法从应用程序目录中获取图像,可以采用相同的方法,但图像应该在应用程序目录之外,您可以使用自己的目录喜欢资产。

application目录下有htaccess文件,拒绝所有。

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>