This question already has answers here:
Relative URL issue
(3 answers)
Closed 7 years ago.
This is how my header.php looks like;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="headerStyle.css">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js"></script>
<script src="headerScript.js"></script>
<?php
if( strpos($_SERVER['PHP_SELF'], 'index') ){
echo '<title>My website title</title>';
echo '<link rel="stylesheet" type="text/css" href="index.css">';
echo '<script src="index.js"></script>';
}
if( strpos($_SERVER['PHP_SELF'], 'index') ){
echo '<title>Contact Us</title>';
}//And goes on like this..
?>
</head>
header.php,headerStyle.css,headerScript.js are in same folder : mysite/header/
And this how my index.php looks like;
<?php
include 'header/header.php';
//And some other irrelevant stuff (<body> etc.)
?>
My index file's path is mysite/
The problem is when I view the index page it cannot load headerStyle.css and headerScript.js.
What is the best way to manage that?I'm trying to make a solid,dynamic header.php so i can include it for every page in my site.
You can define a constant and make use of __DIR__ magic constant.
define('HEADER',__DIR__.'/');
<link rel="stylesheet" type="text/css" href="<?php echo HEADER; ?>headerStyle.css">
<script src="<?php echo HEADER; ?>headerScript.js"></script>
Related
I have a site in php with account. I want to write a CSS for "profile.php/user" (for example: profile.php/benjamin). I tried to put this line in profile.php :
<link rel="stylesheet" type="text/css" href="profile.css">
But obviously doesn't work.
profile.php
<?php
session_start();
if($_SESSION['logged'] == true){
$username = $_SESSION['login_username'] ;
$username = strtoupper($username); //il facem uppercase
echo "Welcome ".$username;
echo "</br>Press here to go to the home page";
echo "</br>Click <a href='/logout.php'> here </a> to log out.";
}
else {
echo " </br></br></br>You must login first to see this section ";
header("refresh:4;url=/index.php");
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--scalare pagina in functie de dispozitiv -->
<link rel="stylesheet" type="text/css" href="profile.css">
<html>
<body>
<div class = "top_band"></div>
</body>
</html>
Assuming the CSS file is in the same directory as the script, it should be:
<link rel="stylesheet" type="text/css" href="../profile.css">
This is because as far as the browser is concerned, profile.php/ is a directory in the URL. To get back to that same level, you have to go up with ../ in the URL.
The browser doesn't know anything about the actual file locations, and which parts of the URL are directories, filenames, and parameters, it just parses it textually. / simply separates directory levels, and ../ removes a trailing directory level when parsing a relative URL.
I would like to give a title to each of my pages, but all my pages are linked to my index.php:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Test</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("header.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>
Here is how my site is: http://prntscr.com/47nn7h
All my pages have the title I put for index.php, but how to add a title to a specific page (example, when I go to the page members.php)?
members.php:
<?php include "index.php";?>
Thanks.
Replace the existing <title> tag with this.
<title><?php echo $pagetitle; ?> </title>
in your <head> block.
Make sure that $pagetitle actually contains the desired title before you emit the tag. It's not clear from your question where these titles are coming from - you'll probably need some PHP right at the top of the page to set all this up.
.
You could use this method and add the
$pageTitle = 'Title of Page';
to your content page (i.e. member page)
I have a php include statement for the "head" of my website.
I am using the following code to call head.php...
<?php
include '../components/head.php'
?>
And in my head.php I have the following code...
<head>
<link rel="stylesheet" type="css" href="/style.css">
<title>Dummy Code</title>
</head>
How can I make it change the title by having a variable in my page on my page like Dummy Code | About being the title if I have $title = "About" on my webpage.
Is there anyway to do this?
They all belong to the global namespace, so you just can do this:
<?php
$title = 'about';
include '../components/head.php'
?>
head.php:
<head>
<link rel="stylesheet" type="css" href="/style.css">
<title>Dummy Code | <?=$title; ?></title>
</head>
But make sure that you understand: this is very simplified code and should not be used on the production projects.
I have a index.php file which loads (require) 2 different files based on a condition.
Visiting this link will cause;
mysite.com/index.php will load stats.php (require("stats.php");)
Visiting this link will cause;
mysite.com/index.php?auth="jgbsbsbasm" will load encry.php (require("stats_encry.php");)
Done with this code:
<?php
if(isset($_GET['auth'])) require("stats.php");
else require("stats_encry.php");
?>
This works fine. Now my question is; I have a CSS file in the header as static;
<link rel="stylesheet" href="cv.css">
What I want is now to load cv.css file for stats.php and cv1.css for stats_encry.php respectively.
How do I do this?
In your header replace
<link rel="stylesheet" href="cv.css">
with
<?php if(isset($_GET['auth'])){ ?>
<link rel="stylesheet" href="cv.css">
<?php } else { ?>
<link rel="stylesheet" href="cv1.css">
<?php } ?>
You can use this like below syntax
<?php
if(isset($_GET['auth'])){
echo '<link rel="stylesheet" href="cv.css">';
} else {
echo '<link rel="stylesheet" href="cv1.css">';
}
?>
My idea is to switch multiple links to CSS files, if special URL was detected. But I have a trouble: my code includes only css in the first if..else statement. And it doesn't depend on the URL.
Here is my code.
http://pastebin.com/Jm3QFDmH
ported from pastebin
<?php
// get first folder in URL
$f_folder = substr(substr($_SERVER["REQUEST_URI"],1), 0, strpos(substr($_SERVER["REQUEST_URI"],1), "/"));
//get full directory structure from URL for current page
$full_path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
//css for account
if ($f_folder='account') {
?>
<link rel="stylesheet" href="/mainstyle/common.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="/mainstyle/account.css" type="text/css" charset="utf-8" />
<?php
} elseif ($f_folder='signin'||$f_folder='signup'||$full_path='/account/resetPassword'||$full_path='/account/logout') {
?>
<link rel="stylesheet" href="/mainstyle/login-signup.css" type="text/css" charset="utf-8" />
<?php
} else {
?>
<link rel="stylesheet" href="/mainstyle/common.css" type="text/css" charset="utf-8" />
<?php
}
?>
Where is mistake?
if and elseif, should have == comparison operator , not = assignment
Line 7 should be
if ($f_folder=='account') {