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.
Related
I'm getting weird results when trying to kind of make a "templating engine". Basically, I want to be able to use PHP variables that contain data from an SQL database.
What happens is that everything works properly with the PHP side, what does not is the page that needs to display this information (index.php).
I'm working on a way to get the website's name from the sql database, so I have something like that on my index:
<?php
include ('php/data/sitename.php');
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $sitename; ?> - Home</title>
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Let browser know website is optimized for mobile-->
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div class="container">
<!-- HEADER: Navbar -->
<?php $navbar; ?>
<!-- MAIN: Index Page contents -->
<?php $page_index ?>
<!-- FOOTER: Footer -->
<?php $footer; ?>
<?php $sitename; ?>
</div>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
</body>
</html>
This variable comes from a file (that has been included) called sitename.php, with the following code:
<?php
include ('../db.php');
$sql = "SELECT id, sitename FROM GeneralData";
$getname = mysqli_query($conn, $sql);
if ($getname->num_rows > 0) {
while($row = $getname->fetch_assoc()) {
$sitename = $row['sitename'];
echo $sitename;
}
}
?>
Yes, I used echo $sitename;, I know it wont echo the actual data, but I did it to test some things, and here are the results:
Including the file sitename.php to index.php will do nothing, it would be like if it did not exist. However, if I write "echo "123";" on it, it will echo 123 on index. What does not work is what I need.
If I go to sitename.php directly, it will simply output the correct SQL value I requested because I told it to echo (as I stated before). But, it wont work in index, it will simply not work.
Also, I'll leave my project structure here. It might help.
What can I do?
Thanks in advance!
try set GLOBAL for sitename
GLOBAL $sitename;
or
GLOBALS['sitename'];
$sitename = ...
EDIT
try use
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/yourfile.php";
include_once($path);
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>
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') {