This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I have programmed a web application, which requires you to login first, before having access to the actual content of the application.
I am setting the value 'logedIn' to false when a user enters the homepage like this:
<?php
$_SESSION['logedIn'] = "false";
?>
If the user tries to go to overview.php, the code checks if the value of logedIn equals 'true'. If it is not, a header relocates the window to the login page like this:
<?php
session_start();
?>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
<script src="../javascript/overview.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/mainpage.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
<?php
if($_SESSION['logedIn'] != "true") {
header("Location: ../index.php");
exit();
}
?>
<div class="container-fluid z-container">
<div class="row z-overview-top-menu">
<div class="col-md-2 z-background-dark z-top-nav z-border-right" id="menu_logo">
<h3>Logo</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right z-selected" id="menu_diary" onclick="changeMenu('diary')">
<h3>Dagboek</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_pazo" onclick="changeMenu('pazo')">
<h3>Pazo</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_counter" onclick="changeMenu('counter')">
<h3>Tellers</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_overview" onclick="changeMenu('overview')">
<h3>Overzicht</h3>
</div>
<div class="col-md-2 z-top-nav z-item-hover" id="menu_comparison" onclick="changeMenu('comparison')">
<h3>Vergelijking</h3>
</div>
</div>
<div class="row">
<div class="col-md-2 z-overview-left-menu">
<ul class="z-left-list" id="userList"></ul>
</div>
<div class="col-md-10 z-overview-main-menu">
<ul id="overviewList" class="z-overview-list-main">
</ul>
</div>
</div>
</div>
</body>
When I replace the header with an echo, I get a response, indicating that the header is indeed executing when I run the code.
Also there are no .htaccess in any of the directories.
Any thoughts on what the problem might be?
http://php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
Since you are outputting:
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
<script src="../javascript/overview.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/mainpage.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
before the header call, it will fail.
Related
This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 5 years ago.
IK this is prob a dupe, but I cant find any solutions to my code what so ever, no matter what code i add/change, it just wont budge... (NOTE im not using normal php, using a friends version, which yes, is fine and dandy)
<?php
include($_SERVER['DOCUMENT_ROOT'].'/phpAlphaDB/core.php');
?>
<html>
<head>
<link rel="icon" type="image/png" href="../logo.png">
<title> Xenoz Web - Users </title>
<link rel="stylesheet" type="text/css" href="../css/style.php" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<?php
if ($role==0) {
header('Location: http://xenozweb.tk/index.php');
} else {
echo '<script>alert("Hello, '.$username.'. Welcome to the userlist...");</script>';
}
?>
<body>
<div class="navigation">
<div class="navitem"> Home </div>
<div class="navitem"> Register </div>
<div class="navitem"> Login </div>
<div class="navitem"> Users </div>
<div class="navitem"> Jukebox </div>
</div>
</div>
<div class="pagecontent">
<div class="userblock">
<?php
$results = db_read('xenozweb-users', '', 'username');
foreach ($results as $result) {
$u_name = db_column($result, 0);
echo '<div class="users">',$u_name,'</div>';
}
?>
</div>
</div>
</body>
</html>
You're returning a partial response before you set the header. Headers must be sent before a response is sent back to the browser.
Try moving the header('Location: ') function call into the top <?php enclosure like so:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/phpAlphaDB/core.php');
if ($role==0) {
header('Location: http://xenozweb.tk/index.php');
}
?>
<html>
<head>
<link rel="icon" type="image/png" href="../logo.png">
<title> Xenoz Web - Users </title>
<link rel="stylesheet" type="text/css" href="../css/style.php" />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<?php
if ($role != 0) {
echo '<script>alert("Hello, '.$username.'. Welcome to the userlist...");</script>';
}
?>
</head>
<body>
You've already sent data to the browser. you cannot send header() info properly once you've already started to send data payload.
I have some big problems including my header in subfolder. I have a folder structure like this:
[![Folder structure][1]][1]
I would like to include my header in the innovatian-project.php. As I see it, I have to go back three folders to get to the root:
AU -> Courses -> Document Root
I have tried with the following:
<?php include '../../../resources/includes/header.php'?>
<?php include(__DIR__.'/resources/includes/header.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php'); ?>
<?php include($_SERVER['DOCUMENT_ROOT'].'/resources/includes/header.php'); ?>
<?php include('../../resources/includes/header.php'); ?>
But none of this is working. I have tried so many things, that I cannot even remember anymore. Does anybody have an idea on what I can do? The path to the innovation-project is here:
<?php include 'resources/includes/header.php' ?>
<body id="services">
<?php include 'resources/includes/navbar.php' ?>
<!-- start intro section -->
<section class="intro">
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<div class="intro-content">
<h1>Mine kompetencer <strong>strongest </strong></h1>
<h2>areas, where I can add the most value to a company </h2>
Mine kompetencer er baseret på
</div>
</div>
</div>
</div>
</section>
<!-- end intro section -->
UPDATE
When I use the following code, the header, nav and footer is called. But the CSS is not called:
<?php include $_SERVER['DOCUMENT_ROOT'] . '/Portfolio_da/resources/includes/header.php' ?>
<body id="services">
<?php include $_SERVER['DOCUMENT_ROOT'] . '/Portfolio_da/resources/includes/navbar.php' ?>
<!-- start intro section -->
<section class="intro">
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<div class="intro-content">
<h1>Mine kompetencer <strong>strongest </strong></h1>
<h2>areas, where I can add the most value to a company </h2>
Mine kompetencer er baseret på
</div>
</div>
</div>
</div>
</section>
<!-- end intro section -->
<?php include $_SERVER['DOCUMENT_ROOT'] . '/Portfolio_da/resources/includes/footer.php' ?>
UPDATE 2 WHERE I CALL THE CSS IN header.php
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Favicon -->
<link rel="shortcut icon" href="images/favicon.ico">
<!-- Stylesheets -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.css">
<link rel="stylesheet" href="css/owl.theme.css">
<link rel="stylesheet" href="css/owl.carousel.css">
<link rel="stylesheet" href="css/supersized.css">
<link rel="stylesheet" href="css/nivo-theme.css">
<link rel="stylesheet" href="css/nivo-lightbox.css">
<!-- Main Stylsheets -->
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/responsive.css">
<!-- Theme Color Stylesheet -->
<link rel="stylesheet" href="css/theme_color.css">
<!-- Google Font -->
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<?php include_once("analyticstracking.php") ?>
</head>
<?php include($_SERVER['DOCUMENT_ROOT'].'/resources/includes/header.php'); ?>
See your php.ini about
allow_url_include
What is in your router file?
You have to go up 2 folders.
<?php include('../../resources/includes/header.php'); ?>
UPDATE
Change these lines in header.php: <link rel="stylesheet" href="css/bootstrap.min.css">
like <link rel="stylesheet" href="http://<?php echo $_SERVER['HTTP_HOST'] ?>/Portfolio_da/css/bootstrap.min.css">
I have recently setup a Fedora 20 VPS with LAMP. My PHP is working properly as you can see from output below:
http://talkoot.co.in/info.php
My problem is when I access page below, the php script is printed out instead of being executed. If one 'Views Source' then the entire code is visible.
http://talkoot.co.in/WebBackend/admin/login.php
Can someone help me out. Here is the code for login.php:
<?php
include 'init.php';
if(isset($_SESSION['admin'])){
header('Location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<title>Login - CPanel</title>
<link type="text/css" rel="stylesheet" href="libs/css/materialize.min.css" media="screen,projection"/>
<link type="text/css" rel="stylesheet" href="libs/css/style.css" media="screen,projection"/>
</head>
<body>
<div class="container">
<div class="card publish-form-container">
<?php
if(isset($_POST['username'],$_POST['password'])){
$__USERS->adminLogin($_POST);
}
?>
<form class="col s12" method="POST" action="" >
<div class="input-field">
<input id="username-field" type="text" name="username">
<label for="username-field">Username...</label>
</div>
<div class="input-field">
<input id="password-field" type="password" name="password">
<label for="password-field">Password...</label>
</div>
<div>
<button class="btn waves-effect waves-light pull-right" type="submit">Login
<i class="mdi-content-send right"></i>
</button>
</div>
</form>
</div><!-- Post Card //-->
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="libs/js/materialize.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.materialboxed').materialbox();
});
</script>
</body>
</html>
Your server is not enabled for Short Open Tag there are 2 scenarios
enable it from php.ini short_open_tag = On or using htaccess
Use <?php instead of <?
P.S: i have shared a link which can explain you why i presented 2 options
As I can see in your phpinfo page:
short_open_tag Off Off
that means the short open tag are now allowed
So, anything you put inside short tags <? ?> won't be parsed as PHP. You'd rather put it inside standard tags <?php ?> block.
The file location of the image is correct and it displays on localhost however doesn't online, how would i be able to get this to work?
I have tried changing the images from png to jpg and using different images and changing the file locations and then updating that on the code however it still does not display the image
<!DOCTYPE HTML>
<html>
<head>
<title>KMS</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript" src="http://s.sharethis.com/loader.js"></script>
</head>
<body id="wrapper">
<div id="header">
<!--Logo-->
<div id="Logo">
</div>
<!--Logo-->
<!--Menu Buttons-->
<?php include 'menu.php';?>
<!--Menu Buttons-->
</div>
<div id="hContent">
<!--Frames--====================================================================-->
<div id="hFrames">
<!--Information go below-->
<div id="infoCon1">
<img id="kms_img" src="Images/Kmsproductsinformation.jpg" style="width:850px;"/>
<b />
<b />
</div>
<!--Information end-->
</div>
</div>
</div>
<!--
footer
-->
</body>
</html>
Try <img id="kms_img" src="images/kmsproductsinformation.jpg" style="width:850px;"/> and ensure that your images directory and kmsproductsinformation.jpg file are in lowercase.
You've got something funny going on with your DIVS. You have what looks like two additional close DIV tags () towards the end of your file - I'd start by fixing those up.
Having said that it also shouldn't really matter if it's localhost or not, or whether it's jpg or png but PNG would be better if you're setting it to a non-100% width.
Try something basic without all the DIVS or your menu.php file and see if that works.
<!DOCTYPE HTML>
<html>
<head>
<title>KMS</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript" src="http://s.sharethis.com/loader.js"></script>
</head>
<body id="wrapper">
<img id="kms_img" src="Images/Kmsproductsinformation.jpg" />
</body>
</html>
Hi i had an issue reported for my script (https://github.com/joshf/Indication/issues/6) about echo statements printing rather than excecuting
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Indication</title>
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="resources/bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
}
</style>
<link href="resources/bootstrap/css/bootstrap-responsive.css" type="text/css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<!-- Nav start -->
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Indication</a>
</div>
</div>
</div>
<!-- Nav end -->
<!-- Content start -->
<div class="container">
<div class="page-header">
<h1><? echo WEBSITE; ?></h1>
</div>
<div class="alert alert-error"><h4 class="alert-heading">Error</h4><p>ID cannot be blank.</p><p><a class="btn btn-danger" href="javascript:history.go(-1)">Go Back</a></p></div></div></body></html>
Notice the echo WEBSITE
The PHP code is
die("<div class=\"alert alert-error\"><h4 class=\"alert-heading\">Error</h4><p>ID does not exist.</p><p><a class=\"btn btn-danger\" href=\"javascript:history.go(-1)\">Go Back</a></p></div></div></body></html>");
!
Is this a code issue or a configuration issue, im unable to reproduce it
This is probably because you are using php short tag <?..?>. Please avoid using them and switch to long one <?php....?>
Probably you have turned off short tags in the server.
It is not a good idea to use
<? /* something */ ?>
<?=echo $something;?>
Because in some servers short tags are disabled.
Try:
<?php echo WEBSITE; ?>