dynamically include header.php file that hold js and css resources - php

I have a question about the inclusion of an header file using PHP.
The code above is the header that I include in all my HTML/PHP view files. I want to save it inside a file called as a naming convention header.php.
My doubt is about the resources loading.
How I can load the needed stylesheets and js files if the header is included from another folder that isn't the same that hold the main css and js folders?
I want to avoid the duplication of the basic resources, and avoid having console errors due to the position of the resources files.
<!DOCTYPE html>
<html lang="it">
<head>
<!-- basic meta tag -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="author" content="" />
<!-- add facebook og tag here -->
<meta property="og:url" content="" />
<meta property="og:type" content="article" />
<meta property="og:title" content="" />
<meta property="og:description" content="" />
<meta property="og:image" content="" />
<!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' https://masserianobile.it; font-src 'self' https://fonts.gstatic.com/; img-src 'self'; style-src 'self' https://fonts.googleapis.com/;"> -->
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="css/fontawesome-all.min.css" type="text/css">
<link rel="stylesheet" href="css/app.min.css" type="text/css">
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script src=""></script>
</head>

Here is what I use to autoload my PHP classes for any nested directory, this can work for your Front-End resources too:
<?php
/* the root directory of your project
can be useful when working on local env
must not end with a slash, leave it empty if not needed */
const ROOT_PATH = '/projects/some_stuff';
// Get parts of the URL without the root path
$rel_path = explode('/', str_replace(ROOT_PATH, '', $_SERVER['SCRIPT_NAME']));
// Remove empty items from the parts
$rel_path = array_filter($rel_path);
// If the last part is a php file, remove it, we don't need it
if (strpos(end($rel_path), '.php') !== false) { array_pop($rel_path); }
// Build the "../" prefix
$prefix = implode('/', array_fill(0, count($rel_path), '..'));
$prefix = empty($prefix) ? '' : $prefix . '/';
?>
<!-- just output the prefix before the resource URL -->
<link rel="stylesheet" href="<?php echo $prefix; ?>css/bootstrap.min.css" type="text/css">
Documentation:
$_SERVER Server and execution environment information
array_filter() Filters elements of an array using a callback function
end() Set the internal pointer of an array to its last element
array_pop() Pop the element off the end of array
array_fill() Fill an array with values

Related

Load skin CSS internally instead of a link in HTML head on wordpress

I am trying to load the skin CSS in the head of the page in <style></style> tags instead of linking to it as <link href="/wp-content/thesis/skins/nina-cross-promo/css.css" rel="stylesheet" />, but so far without much luck.
Can you please recommend a function for my custom_functions.php so that I can grab the contents of that CSS file, minify it with:
function minimizeCSSsimple($css){
$css = preg_replace('/\/\*((?!\*\/).)*\*\//','',$css); // negative look ahead
$css = preg_replace('/\s{2,}/',' ',$css);
$css = preg_replace('/\s*([:;{}])\s*/','$1',$css);
$css = preg_replace('/;}/','}',$css);
return $css;
}
and then insert in the head as <style>My minified css</style> so it will look like this:
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>My minified CSS</style>
and not like this:
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="/wp-content/thesis/skins/nina-cross-promo/css.css" rel="stylesheet" />
Please help
You can add your minified css within site head tag by as follows -
add_action( 'wp_head', 'add_custom_styles' );
function add_custom_styles(){
?>
<style type="text/css">
<?php
$custom_css = "Your custom css goes here";
echo $custom_css;
?>
</style>
<?php
}
Add this codes in your active theme's functions.php

response string contains <?php

I'm sending a request for an OAuth service:
$headers = array(
'Content-type' => 'application/x-www-form-urlencoded'
);
$request = new cURLRequest();
$request->setMethod(cURLRequest::METHOD_POST);
$request->setHeaders($headers);
//#######TO ADD:
//#######SLUG TEST
$fields = array(
'redirect_uri' => '******custom_cdr/did_conversion/did_conversion/pages/authentication.php',
'response_type' => 'code',
'client_id' => urlencode($config['OAUTH_APP_ID']),
'state' => '0'
);
$request->setBody($fields);
$response = $request->sendRequest($reqUrl);
$respBody = $response->getBody();
if ($response->getStatus() == Response::STATUS_OK) {
eval($respBody);
};
the response is html (persumably authentication) which starts with php container which I don't know how to print / implement.
The code is not redirecting any where:
<?php
$nonce = base64_en
code(mcrypt_create_iv (16, MCRYPT_DEV_URANDOM));
header("Content-Security-Policy: frame-ancestors 'self' *.hubgets.com; script-src 'self' 'nonce-$nonce' 'sha256-uux/wJCjHlmkdiZXIIoFij3+K7FDO0+C0AJ3Yu+OV4w=' 'sha256-NQYHdO+DXSEnKRsq/w9j6GWfOc5gx1XV7TMQO0NAVFA=' 'sha256-Iw8FmsFJDf8FWS5y6AqfZDJiDVU6yqI7sDVnk8uBj6w=' https://tagmanager.google.com https://js.stripe.com https://www.google-analytics.com https://ssl.google-analytics.com https://www.googletagmanager.com https://apis.google.com https://www.googleadservices.com/pagead/;");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<title>Authorize App</title>
<meta name="description" content="Hubgets is a communication and collaboration service that instantly makes your team more collaborative."/>
<meta name="keywords" content="Hubgets, communication, collaboration, telephony, instant messaging, presence, mobility, chat, conference, information management"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport"
content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta name="apple-mobile-web-app-title" content="Hubgets" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
<meta name="format-detection" content="telephone=no">
<!-- Chrome, Firefox OS and Opera -->
<meta name="theme-color" content="#0ea5db">
<!-- Windows Phone -->
<meta name="msapplication-navbutton-color" content="#0ea5db">
<!-- iOS Safari -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<!-- Browser Favicon -->
<link rel="icon" href="./skin.8aef9f1123badd773f06e869e5cb22a140face8a/favicon.ico" />
<!-- Browser Favicon - Firefox Fix -->
<link rel="icon" href="./skin.8aef9f1123badd773f06e869e5cb22a140face8a/favicon.ico" />
<link rel="stylesheet" type="text/css" href="./skin.8aef9f1123badd773f06e869e5cb22a140face8a/css/hf.css">
it also returns a 404 after trying to find a file located in './' compared to a location I don't have on my server

User not redircted back to last visited page after logged out

In my website I am trying to redirect uers back to the last page they visited after they log back in but I am not getting it. I used cookie to track their last visited page, and I created a redirect page to check if the cookie still exist and if it does it should take them to the last page they left off.
here is the header of my website which I included to all the pages:
include('../includes/admins.php');
// Set your cookie before redirecting to the login page
$current_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$_COOKIE['redirect_to'] = $current_page;
$cookie_name = $_COOKIE['redirect_to'];
$cookie_value = $current_page;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<meta name="description" content="Automating your school work.">
<meta name="author" content="<?php echo $set['siteName']; ?>">
<meta name="keyword" content="content, management, system, schools, school management system, schoool portal, chat, link, linkedln, <?php echo $set['siteName']; ?>">
<link rel="icon" href="<?php echo $set['installUrl'].'logo/'.$set['schoolLogo']; ?>" type="image" />
<title><?php echo $fullname; ?></title>
<!-- Favicon-->
<link rel="apple-touch-icon" sizes="57x57" href="images/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="images/apple-icon-60x60.png">
<link rel="manifest" href="images/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="images/ms-icon-144x144.png">
<meta name="theme-color" content="">
<!-- Google Fonts -->
<link href="../afiles/css/font.css" rel="stylesheet" type="text/css">
<link href="../afiles/css/icon.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="../afiles/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- Bootstrap Core Css -->
<link href="../afiles/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="../afiles/css/bootstrap-material-datetimepicker.css" />
</head>
If you look at the top you will see I include the cookie.
Here is the redirect page to see if cookie exist:
<?php
include('../includes/functions.php');
if(!isset($_COOKIE[$cookie_name])) {
header("Location: main");
} else {
header("Location: ". $cookie_name);
}
?>
Need someone to point me to the right direction.
In your second block of code you have $_COOKIE[$cookie_name] but since you haven't defined $cookie_name in your file, !isset($_COOKIE[$cookie_name]) will always return false. Possible solutions:
Replace
if(!isset($_COOKIE[$cookie_name])) {
with
if(!isset($_COOKIE['redirect_to'])) {
To match the index value of the first block code.
Second option, if you think you might change redirect_to value, is to define a constant in a separate file and then use it in both of your blocks.

Writing Head section with PHP

I'm converting a couple sites from HTML to PHP for dynamic elements and have been able to do so with the header and footer (using php include()). However, I'm confused on how to do the head section. This is what I have with plain HTML:
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="Liberty Resource Directory. The ultimate curated directory to find what you need."/>
<meta name="keywords" content="ethan glover, lrd, liberty resource directory"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title>Liberty Resource Directory</title>
</head>
I can easily add the HTML5Shim script, meta charset, viewport (yes I will remove that max scale), and the stylesheet link.
Here's the problem:
How can I write the .php file in a way that I can pass an individual pages description, keywords and title to it? (That way I can put the entire above code in a php file and just include it on every page.)
Or will I simply have to exclude the description, keywords and title, and rewrite those parts every time?
Here's the answer: (Courtesy of Alejandro Arbiza)
head.php
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="<?php echo $description;?>"/>
<meta name="keywords" content="<?php echo $keywords;?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title><?php echo $title;?></title>
</head>
index.html (to include above code)
<?php
$description="Liberty Resource Directory. The ultimate curated directory to find what you need.";
$keywords="ethan glover, lrd, liberty resource directory";
$title="Liberty Resource Directory";
include 'scripts/head.php';
?>
The final result:
http://libertyresourcedirectory.com/
You can use variables for the description and the keywords (or anything else you want for that matter). Then, when the time comes to construct the page, you would just set the variables with the corresponding values.
<head>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
<meta charset="UTF-8" />
<meta name="description" content="<?php echo $description; ?>"/>
<meta name="keywords" content="<?php echo $keywords; ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="stylesheets/lrdstylesheet.css" rel="stylesheet" media="screen">
<title>Liberty Resource Directory</title>
</head>
So, lets say you have page1.php and page2.php:
<?php
// page1.php
$description = "This is page one";
$keywords = "page one";
include 'header.php';
?>
<!-- Page content -->
<?php include 'footer.php'; ?>
and
<?php
// page2.php
$description = "This is page two";
$keywords = "page two";
include 'header.php';
?>
<!-- Page content -->
<?php include 'footer.php'; ?>
Of course, I am assuming here that the whole HTML header is inside the header.php file, that is including <html>, <head> and <body>.

php included file execution failure

Good afternoon everyone. I'm stuck on including a file that has all of my 'head' information in it. I wanted to break down my page so it's simpler and less complicated. in my index.php file i did this;
<?php
include 'Resources/includes/head.php';
?>
It finds the file and i know this because in dreamweaver when i go on my index page it comes up as a tab underneath it. However when i view my index page on the local server, it's as if none of the styles or scripts have been applied to the page. This is the code that my head.php file has in it.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="description" content="" />
<meta name="keywords" content=""/>
<link rel="shortcut icon" href="../images/design/icon.png" type="image/x-icon"/>
<link rel="stylesheet" href="../css/style.css" type="text/css" media="screen"/>
<script src="../js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="../js/scripts.js" type="text/javascript"></script>
</head>
You may try something like this by using a base path
<?php
$basepath="http://www.mysite.com/path_to_theme/";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="description" content="" />
<meta name="keywords" content=""/>
<link rel="shortcut icon" href="<?php echo $basepath; ?>/images/design/icon.png" type="image/x-icon"/>
<link rel="stylesheet" href="<?php echo $basepath; ?>/css/style.css" type="text/css" media="screen"/>
<script src="<?php echo $basepath; ?>/js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="<?php echo $basepath; ?>/js/scripts.js" type="text/javascript"></script>
</head>

Categories