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

时间:2016-02-23 09:25:18

标签: php css codeigniter background-image codeigniter-3

几天前我开始使用codeigniter而且我无法通过css文件设置背景图片。我的控制器(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文件必须位于特定的文件夹中(我看过很多教程,但并非所有人都说同样的事情)。我做的最后一次尝试是将文件放在“视图”中,如图enter image description here

所示

我希望我足够清楚,我知道,这是因为我对这个框架很新。

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

enter image description here

更新:

这是我从控制台获得的:

enter image description here

5 个答案:

答案 0 :(得分:1)

像这样改变你的代码:::

<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>

答案 1 :(得分:0)

试试这个

<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>

答案 2 :(得分:0)

使用Codeigniter URL Helper 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;
}

体{ background-image:url(); 背景颜色:绿色; }

希望这会对你有所帮助。如果我误会,请随意提问......或者随意评论;

请注意我在这里编码;我没有测试过它..但我已将它应用于我的项目..玩得开心!

答案 3 :(得分:0)

   body{
         background: url(<?php echo base_url('assets/images/bg_images.jpg');?>);
}

答案 4 :(得分:0)

"C:/wamp/www/CI/application/views/immagini/Opedia_LAB.png"

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

在应用程序目录中有htaccess文件,它拒绝所有。

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