i have a serious problem over here. I created a user system with sessions.
The problem is that there is content you only see when you are logged in. For example in the navigation bar the sign in button is replaced with a account button.
Now to my problem:
Every page php-includes the navbar.php.
For example in the index.php is written:
<body>
<?php include("navbar.php")?>
</body>
The login.php redirects to the index.php:
header("Location: index.php");
But the index.php does not refresh. After a hard refresh with "F5" every thing is fine.
I also tried meta tags to prevent loading the page in the cache.
Any Ideas?
index.php:
<html lang="en">
<head>
...
</head>
<body >
<?php include("navbar.php")?>
<div id="wrap">
...
</div>
</body>
</html>
navbar.php
<div class="navbar">
<?php
session_start();
if (!isset($_SESSION['logged']) || !$_SESSION['logged'])
{?>
...Sign in etc...
<?php
}
else
{?>
...Accounting...
<?php
}?>
</div>
login.php:
<html>
<head>
</head>
<body>
<?php include("navbar.php"); ?>
<div class="container">
<form class="form-signin" action="logon.php" method="post" >
...
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
logon.php:
<?php
if login successfull //pseudo code
header("Location: http://www.***.com/index.php");
}
else
{
header("Location: http://www.google.de");
}
exit;
?>
add this to your .htaccess file. this will disable browser caching on these file extensions
<FilesMatch ".(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
According to the specs, you have to pass an absolute uri.
It's also best to add this, when redirecting:
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.google.com');
For SEO purpouses, and browser cache (google this to find out more).
I hope you know this already, but you'll also have to make sure no output has been sent to the client, because in that case, the headers have already been sent, and logic dictates that it's too late to change them, then.
Check this question for more details on how to deal with headers and output buffering.
Related
I am creating my first website from scratch and had seen something where you can reduce code by using PHP includes for sections of the site that are to be repeated. So far, I have a head.php (which I added due to my stylesheet.css being linked there and needing access to it on every page), header.php, footer.php, index.php, and other pages with the php extension (about, contact, that bunch).
Everything is appearing where I'd like it to except for one issue: when setting the body background color, everything (all includes: header.php, footer.php) seems to be in the body. I tested this by setting a border around the body, and it confirmed what I thought.
Does anyone have an idea what is going wrong? I am using flexbox in my header, footer, and other bits within the index file, but I don't think that should be affecting anything.
<!DOCTYPE html>
<?php include 'head.php'; ?>
<?php include 'header.php'; ?>
<body id="main-block">
<!-- Button links to Portfolio and Other stuff -->
<div class="flex-container">
<a class="main-button" href="#">Web Work</a>
<a class="main-button" href="#">Other Work</a>
</div>
</body>
<?php include 'footer.php'; ?>
</html>
your body tag is suppose to wrap around your header and footer elements. see below markup
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php include (header);?>
<?php include (footer);?>
</body>
</html>
if u wanna change the background color of your elements just add css
element { background-color:pink }
I admit that I do not really understand your issue. But I'll give you a little snippet to start coding php:
<html>
<head>
<title>Welcome</title>
<style>
<?php include 'stile.php'; ?>
</style>
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
</body>
</html>
<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
<?php
header('Location: http://www.google.com');
?>
</body>
</html>
I uploaded this code to my server(1 and 1). Its not redirecting. I used the same code in XAMPP and it worked fine.
What am I doing wrong? Why is it working on xampp and not on real (1and1) server? I appreciate any help.
Thank you
You can not call header function after output. it shows warning as "header already sent...".
you must write it before tag.
<?php
header('Location: http://www.google.com');
?>
<html>
<head>
<title>Location redirect test page</title>
</head>
<body>
</body>
</html>
*may be your server setting for showing warning is off so it wont display warnings to you on related server.
If by some reason you can't place the php snippet before the html code (like Mayur said) you can use JavaScript for redirects
window.location.href = "http://stackoverflow.com";
Remember that when setting headers from php or setting cookies no text must be out putted before the php command!
I'm new to PHP and I'm facing some problems when working with PHP sessions
Let's say I have a file (index2.php) with this code in it.
<?php
session_start();
$_SESSION['name'] = 'The User';
?>
Click
And this is index3.php
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
For some reason I don't understand, index3.php doesn't show anything. What am I doing wrong?
Thanks!
In index3.php you need to start the session as well. As per the official PHP docs:
When session_start() is called or when a session auto starts, PHP will
call the open and read session save handlers.
Using your example, just initiate session_start() as follows:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
Make sure you also have session_start(); in all php pages where you want to retain and work with sessions;
make sure index3.php contains session_start();
The question is fairly simple, yet I've been looking around for an hour and found nothing:
make a page that is exactly the same as the home page, but a specific div has altered content
example index.html:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change">I will change</div>
</body>
</html>
so I want to be able to code a page so that it inherits the entire html from the index page (WITHOUT COPYING THE CODE), but a specific div (here with the id #change) to have different content. How would I go about doing this?
You don't really "inherit" code snippets, but I understand that you're trying to reuse the page content. From your posted code, it's hard to tell exactly how the change differs from the index. Is it just a content change or does the index page not have that div?
You have a couple of options. If just the content of the div is changing, you could use the same php page and then use jquery to change the content of the div, so something like
index.php
<? php include("page.php"); ?>
other page
<? php include("page.php"); ?>
// javascript to modify div
You could break the page into chunks and just include them as needed, so you could have a top.php and a bottom.php, and the index page could do
<? php include("top.php"); ?>
<? php include("bottom.php"); ?>
And then your similar page could do something like
<? php include("top.php"); ?>
// custom stuff here
<? php include("bottom.php"); ?>
If neither of these solutions work you could always use a templating engine to create a page template, though that may be a little much for your situation.
I see you have tagged this question in php So, I will give you answer inclusive of php implementation.
Create 3 pages. index.php about.php and foo.php
The objective is to show some content in index.php but all content in about.php
Call this page foo.php
<html>
<head></head>
<body>
<p> Show this in index.php </p>
<?php if($_SERVER['PHP_SELF'] === 'about.php'): ?>
<p> Show this in about.php </p>
<?php endif; ?>
</body>
</html>
Now, all you have to do is ... include foo.php in both pages.
Make the page you want and you can go about doing this:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<?php
if(basename($_SERVER['PHP_SELF'] == "other-page.php")){ ?>
<div id="change">I will change</div>
<?php }else{ ?>
<div id="change">Original div</div>
<?php } ?>
</body>
</html>
That takes the file name and based on that you can change content (if is only for one page, otherwise write a function/class based on that).
There are many ways to do this. Here are two, each with their own advantages and disadvantages.
Firstly, if you don't want to modify the page at all, you can add a small PHP code segment which will include a page passed in through the GET variable. For example
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change"><?php require($_GET['page']); ?></div>
</body>
</html>
would mean that using the URL mypage.php?page=home.php would automatically include the contents of a file called home.php into that div.
Another way to do it is to divide up that page into 2 sections, and including both of them in any other page you use. For example, splitting the code into 2 seperate files, such as
top.php:
<html>
<head>
<title>title</title>
<style type="text/css">
/* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change">
bottom.php:
</div>
</body>
</html>
then in your PHP file you can use the following
require("top.php);
MY CONTENT HERE
require("bottom.php);
remember that you will need to use echo to output html code on this method if it is within <?php and ?> tags
hope this helps.
You can't do this will plain HTML.
To do it in php, first create template file like so: (template.php)
<html>
<head>
<title>title</title>
<style type="text/css">
* css goes here */
</style>
</head>
<body>
<div id="stay">I wont change</div>
<div id="change"><?=$main_content?></div>
</body>
</html>
Now, let's say you want to make a "contact me" page.
<?php
// in contact.php
$main_content = "Contact me at my#email.com
include "template.php";
?>
This will write the contents of template.php to the page and echo out the value of $main_content inside div#change
Now, this is generally frowned upon because managing your variables becomes difficult as the size of the template increases. To keep things sane, use a templating engine as all of the other answers are suggesting.
I want to use php to create a consistent header and footer across my site using the php incluse tags. When creating the header file, do I need the html and body tags or can I just start with the div id="header"....?
What you should worry about is the final outcome of the markup of the site, after you've included everything.
Example:
header.php
<div id="header"></div>
footer.php
<div id="footer"></div>
index.php
<html>
<head></head>
<body>
<?php include('header.php'); ?>
<div id="content"></div>
<?php include('footer.php'); ?>
</body>
</html>
For correct semantics, include the <html> and <body> tags, making sure to close them in the footer.php file
Yes you should. It's a good idea, though, to use variables within the include to set such things as <title>...
<?php
$pagetitle="My page";
include('header.php');
?>
Content here
<?php include('footer.php'); ?>
where header.php is
<html>
<head>
<title><?php echo $pagetitle; ?></title>
<!-- your meta tags etc -->
</head>
<body>
and footer is
<script>/* your javascript includes */</script>
</body>
</html>
You can have a section of HTML in a file like this.
<footer> This is my global footer! </footer>
Then in PHP you can use include() to include that html file. It will render the contents of the file to the output where you include it.
include 'globalFooter.html';
Do I need the html and body tags or can I just start with the div?
No you do not.
Your question is not completely clear, but you seem to be asking whether you need any special tags in the included file. The short answer is no—the included file is inserted into the including file verbatim, so it would contain whatever tags are required to render the header (or footer) you had in mind.
Whatever you do, the result should be valid HTML.
So if your index.php starts with <?php include "header.php" ?> then your header.php file should start with <!DOCTYPE html><html>... Same goes for the end.