Reputation: 229
So I am trying to practice PHP and I am stuck with using headers. I am using xampp in doing this. I have a simple form wherein the user will log in in the "index.php" now when the log in is successful the header function will start and redirect the page to "includes/profile.php". Now here is the problem, after the header I am currently in "includes" folder so when I use other .php or .css files outside includes folder i need to do "../example/example.php". This mess up my paths because my CSS file is in "css/example.css" so i need to put "../". Is there any way to always make the "pointer" go back to the parent directory even after using header so that i dont need to use "../"?
index.php
<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['password']))
{header("Location: includes/profile.php");exit;}
if (isset($_POST['submit'])) {
if($_POST['username']=="junji" && $_POST['password']=="secret"){
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['password'];
$_SESSION['expiry']=time();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
header("Location:includes/profile.php");
exit;
}
else{
$username = "";
$password = "";
session_destroy();
}
}
?>
//end of index.php
now inside the profile.php
<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['password']))
{if(time()-$_SESSION['expiry']<5){?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include_once('head.php');
?>
</head>
<body>
<div class="container">
<h1>Junji's Bio</h1>
<div class="row">
<div class="col-xs-8">
<h2>Content</h2>
<?php
include_once ('content.php');?>
</div>
<form method="POST">
<div class="col-xs-4 sidebar">
<div class="alert alert-success"> <?php print "You are currenly logged in as ";
print '<br><h3>';
print $_SESSION['username'];print "\t".'<input type="submit" class="btn btn-info" name="submit" value="Logout">'; ?></h3> </div>
</div></form>
</div>
</div>
</body>
</html>
<?php if(isset($_POST['submit'])){
session_destroy();
header("Location: ../index.php?msg=You have been successfully logged out!");
exit;
}
}else
{session_destroy();
header("Location:../index.php?msg2=Your session has expired! Please Log-in again.");
exit;
}
}
else
{session_destroy();
header("Location:../index.php");exit;
}
as you can see the includes are directly called and no "includes/example.php" is needed. which means inside my "head.php" i need to make two
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
just so i can get the css to work on both "index.php" and "includes/profile.php"
Upvotes: 0
Views: 1204
Reputation: 1816
You could define a constant containing the absolute root, and include that in each path. If you save your root in a variable or constant, instead of write it directly in your files, it will be easier for you if your root changes in the future.
define("ROOT", "http://localhost/");
include ROOT . 'example/example.php';
Or maybe construct a function to call?
function path($string, $echo = TRUE) {
$root = "http://localhost/";
if($echo === TRUE) {
echo $root . $string;
} else {
return $root . $string;
}
}
path('index.php');
header("Location: " . path('test.php', false));
Upvotes: 1
Reputation: 1263
The way I handle this is to set a level variable at the top of the page:
$level = '';
This would be top level
$level = '../'
This would be for files in a top level folder
$level - '../../';
This would be for files in a sub directory... and so on
Then all you need to do is set the $level
var in the path:
$level.'css/example.css
-- works everytime
NOTE: If you are using .htaccess
you need to call the full url to both .css
and .js
files
Upvotes: 1
Reputation: 329
If you don't mind using an absolute path, Try using "http://localhost/css/example.css"
Upvotes: 2