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.
Related
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
I am developing my personal portfolio . I am facing a problem that is ..
I have placed all the important meta tags in my header.php file and they're visible in the page source code too .. But they are not working . I got to know this when I placed the Meta OG to get the link preview on social sites.
Here's the image of my the website from where I am checking the rich link preview
And here's my website source code image in which you can clearly see the meta tags included
If something isn't clear from the image then here's the link of my website
EDIT : Here's the code of my website's head section
<!DOCTYPE html>
<html lang="en">
<head>
<?php
global $baseUrl;
$baseUrl='https://www.iamosama.cf/' ;
?>
<!-- Basic -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Mobile Specific Metas -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Osama - Web Designer and Developer</title>
<meta name="description" content="Hello , I am Osama . A creative web designer and developer from Karachi , Pakistan" />
<meta name="keywords" content="Web Designer , Web developer , Front end Developer , UI/UX Designer" />
<meta property="og:site_name" content="Osama - The Web Developer" />
<link rel="shortcut icon" href="<?php echo $baseUrl ; ?>images/favicons/favicon.ico" />
<meta property="og:title" content="Personal Portfolio" />
<meta property="og:description" content="Hello , I am Osama . A creative web designer and developer from Karachi , Pakistan " />
<meta property="og:image" content="<?php echo $baseUrl ; ?>images/thumb.jpg" />
<link href="https://fonts.googleapis.com/css?family=Kaushan+Script%7CPoppins:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
<!-- CSS -->
<link rel="stylesheet" href="<?php echo $baseUrl ; ?>css/basic.css" />
<link rel="stylesheet" href="<?php echo $baseUrl ; ?>css/layout.css" />
<link rel="stylesheet" href="<?php echo $baseUrl ; ?>css/blogs.css" />
<link rel="stylesheet" href="<?php echo $baseUrl ; ?>css/ionicons.css" />
<link rel="stylesheet" href="<?php echo $baseUrl ; ?>css/magnific-popup.css" />
<link rel="stylesheet" href="<?php echo $baseUrl ; ?>css/animate.css" />
<!--[if lt IE 9]>
<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Favicon -->
<script type="application/ld+json">
{
"#context": "http://schema.org/",
"#type": "Person",
"name": "Osama",
"alternateName": "Muhammad Osama",
"url": "https://www.iamosama.cf/",
"image": "https://www.iamosama.cf/images/profile.jpg",
"sameAs": "https://www.iamosama.cf/",
"jobTitle": "Web Designer and Developer"
}
</script>
</head>
[i got an error on using a variable ( $page_title ) for echo the page title but it does not work and show me an error ]
[here is the code of header.php file]
<head>
<meta charset="UTF-8">
<title><?php echo $page_title; ?></title>
<meta name="description" content="sodalitesolutions">
<meta name="author" content="sodalitesolutions">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.css'); ?>">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/font-awesome/css/font-awesome.css'); ?>">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/style/slider.css'); ?>">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/style/mystyle.css'); ?>">
</head>
[Error :: function: _error_handler]
Try to add this :
$this->data['title'] = 'test';
$this->load->view('view_name',$this->data);
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>.
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>