如何在 php 的特定页面中加载 CSS
How to load CSS in specific page in php
我想在我的所有网站页面中都有动态 header.php,并且有许多 CSS 文件需要加载。有没有办法为每个页面加载不同的 CSS 文件?例如:
<?php if($title == "title") { ?>
<link href="mycss.css" >
<script src="test.js"></script>
<?php } ?>
为简单起见,您可以为每个页面分配一个页面代码。使用该名称创建脚本。所以你可以这样导入它:
页面中header:
$pageCode = "INDEX";
在页脚中:
<?php
$scriptFileName = "your/path/to/file/".$pageCode.".js";
if(file_exists($scriptFileName)){
echo "<script src='$scriptFileName'></script>";
}
如果需要,对 CSS 应用相同的方法。但是在 header!
您可以创建一个函数(或 class)来回显您的页眉和提要参数作为设置。这样的事情可能会奏效:
function.get_header.php
// This function will render the header
function get_header($settings = false)
{
ob_start();
include("header.php");
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function.get_page_css.php
// This function will act like a pseudo database return
// and will return a series of css links based on input
function get_page_css($var = false)
{
$css['title'][] = "/css/style1.css";
$css['other'][] = "/css/style2.css";
$css['title'][] = "/css/style3.css";
if(!empty($css[$var]))
return $css[$var];
}
header.php
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><?php echo (!empty($settings['title']))? $settings['title'] : "Untitled Page"; ?></title>
<head>
<?php if(!empty($settings['css']) && is_array($settings['css'])) {
foreach($settings['css'] as $link) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo $link; ?>" />
<?php }
}
?>
</head>
index.php
<?php
// Include the header function
include("function.get_header.php");
// Include the css return function
include("function.get_page_css.php");
// Write the header to browser using the get_page_css() function
echo get_header(array("title"=>"This Great Page!","css"=>get_page_css('title')));
?>
<body>...etc.
首先检查这个url路径
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>
或
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url1 = "http://example.com/home";
$url2 = "http://example.com/about";
if (strpos($url1,'home') !== false) { ?>
<link href="mycss.css" >
<?php
} else if(strpos($url2,'about') !== false){ ?>
<link href="mycss2.css" >
<?php }else { ?>
// your defult css file
<?php } ?>
希望你明白了
简单来说,您可以使用下面给出的方法...
<?PHP
$page_name= $_SERVER['PHP_SELF'];
if($page_name=='about.php'){
echo '<link href="about_css.css" type="text/css">
<script src="about_test.js"></script>';
}
if($page_name=='contact.php'){
echo '<link href="contact_css.css" type="text/css">
<script src="contact_test.js"></script>';
}
?>
将此 index.php 文件放入您的 css 文件夹
<?
$title = $_GET['title'];
header('Content-Type: text/css');
switch ( $title ) {
case 'title_1':
include('style_1.css');
case 'title_2':
include('style_2.css');
default:
include('default.css');
}
exit();
?>
然后每当你想打电话给你的 css :
<?
echo '<link href="path/to/css/folder/?title='.$variable.'" rel="stylesheet">'
?>
css 文件夹中的 index.php 将成为 css 需要包含的控制器
这对我来说很好,我将它添加到 _header.php 文件中,该文件包含在我的所有页面中:
<?php
$page_name = $_SERVER['PHP_SELF'];
if($page_name =='/recepiepg/admin/index.php'){
echo '<link rel="stylesheet" href="../css/admin.css" type="text/css"/>';
}
?>
我想在我的所有网站页面中都有动态 header.php,并且有许多 CSS 文件需要加载。有没有办法为每个页面加载不同的 CSS 文件?例如:
<?php if($title == "title") { ?>
<link href="mycss.css" >
<script src="test.js"></script>
<?php } ?>
为简单起见,您可以为每个页面分配一个页面代码。使用该名称创建脚本。所以你可以这样导入它:
页面中header:
$pageCode = "INDEX";
在页脚中:
<?php
$scriptFileName = "your/path/to/file/".$pageCode.".js";
if(file_exists($scriptFileName)){
echo "<script src='$scriptFileName'></script>";
}
如果需要,对 CSS 应用相同的方法。但是在 header!
您可以创建一个函数(或 class)来回显您的页眉和提要参数作为设置。这样的事情可能会奏效:
function.get_header.php
// This function will render the header
function get_header($settings = false)
{
ob_start();
include("header.php");
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function.get_page_css.php
// This function will act like a pseudo database return
// and will return a series of css links based on input
function get_page_css($var = false)
{
$css['title'][] = "/css/style1.css";
$css['other'][] = "/css/style2.css";
$css['title'][] = "/css/style3.css";
if(!empty($css[$var]))
return $css[$var];
}
header.php
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><?php echo (!empty($settings['title']))? $settings['title'] : "Untitled Page"; ?></title>
<head>
<?php if(!empty($settings['css']) && is_array($settings['css'])) {
foreach($settings['css'] as $link) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo $link; ?>" />
<?php }
}
?>
</head>
index.php
<?php
// Include the header function
include("function.get_header.php");
// Include the css return function
include("function.get_page_css.php");
// Write the header to browser using the get_page_css() function
echo get_header(array("title"=>"This Great Page!","css"=>get_page_css('title')));
?>
<body>...etc.
首先检查这个url路径
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>
或
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url1 = "http://example.com/home";
$url2 = "http://example.com/about";
if (strpos($url1,'home') !== false) { ?>
<link href="mycss.css" >
<?php
} else if(strpos($url2,'about') !== false){ ?>
<link href="mycss2.css" >
<?php }else { ?>
// your defult css file
<?php } ?>
希望你明白了
简单来说,您可以使用下面给出的方法...
<?PHP
$page_name= $_SERVER['PHP_SELF'];
if($page_name=='about.php'){
echo '<link href="about_css.css" type="text/css">
<script src="about_test.js"></script>';
}
if($page_name=='contact.php'){
echo '<link href="contact_css.css" type="text/css">
<script src="contact_test.js"></script>';
}
?>
将此 index.php 文件放入您的 css 文件夹
<?
$title = $_GET['title'];
header('Content-Type: text/css');
switch ( $title ) {
case 'title_1':
include('style_1.css');
case 'title_2':
include('style_2.css');
default:
include('default.css');
}
exit();
?>
然后每当你想打电话给你的 css :
<?
echo '<link href="path/to/css/folder/?title='.$variable.'" rel="stylesheet">'
?>
css 文件夹中的 index.php 将成为 css 需要包含的控制器
这对我来说很好,我将它添加到 _header.php 文件中,该文件包含在我的所有页面中:
<?php
$page_name = $_SERVER['PHP_SELF'];
if($page_name =='/recepiepg/admin/index.php'){
echo '<link rel="stylesheet" href="../css/admin.css" type="text/css"/>';
}
?>