This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
I know this has been asked a million times already but I can't seem to find an answer that helps with mine.
I just started a new php website and as you saw in the title it keeps saying:
Warning: Cannot modify header information - headers already sent by (output started at /customers/e/7/8/andersws.dk/httpd.www/template/index.php:2) in /customers/e/7/8/andersws.dk/httpd.www/template/index.php on line 4
All that is in the file so far is:
<!DOCTYPE html>
<?
if(file_exists('first.run')){
header('location: index.php');
}
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
So I really can't see what I'm doing wrong.
It's because you specified a doctype before the header. You cannot output a header after the payload has been sent. So change it to:
<?
if(file_exists('first.run')){
header('location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
</html>
The HTTP headers can only being sent before the body. All output of the page belongs to the body. In your example you are attempting to output the header after the output of the <doctype> node.
Use this:
<?
if(file_exists('first.run')){
header('location: index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
Use this;
<?php
ob_start();
if(file_exists('first.run')){
header('location: index.php');
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
?>
</body>
</html>
<?php ob_end_flush(); ?>
See here for more detail about `ob_start()
Related
This is on file : PurchaseSiteLoggedIn.php
<!DOCTYPE html>
<html lang="en">
<header>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palm User Login-Registration</title>
<link rel="stylesheet" href="">
</header>
<script src=""></script>
<body>
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
</body>
</html>
If the user is not logged in he/she will be redirected to another page.(the loginregister.html)
This code works fine. What I wanna do is replace:
<?php
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
?>
with DoAnonymousCheck(); (a random name for the function) so that the code looks cleaner
//
Ideally i would want to have the body of the DoAnonymousCheck on a different file.
I tried somthing like:
I added
<script src='DoAnonymCheck.php'></script>
in the PurchaseSiteLoggedIn.php folder.
And in another folder that i called DoAnonymCheck.php I had
<?php
function DoAnonymCheck(){
session_start();
if(isset($_SESSION['id']) && isset($_SESSION['username'])){
}
else{
header("Location: http://localhost/loginregister.html");
}
}
?>
It didnt work though (i guess in <script src></script> you can only add a .js folder)
You can't "import" a PHP script with a script tag, because all PHP code is executed on the server side before it shows up in the client browser. However, you can use include or require to load other PHP scripts. Code Example
Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try
Is there any way to get PHP string value before it was setted. Maybe it's possible to make with substr_replace?
I need to write this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $monkey;></title>
</head>
<body>
<?php
$monkey = "I like banana!";
echo $monkey;
?>
</body>
</html>
To get this result:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>I like banana!</title>
</head>
<body>
I like banana!
</body>
</html>
I know I could set $monkey before <html> code, but can I declare $monkey at the end of page to get it at the beginning?
UPDATE:
This example not working:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php title();?></title> <!-- Need result I like banana! -->
</head>
<body>
<?php
$monkey = "I like banana!";
function title(){
echo "$monkey";
}
title();
?>
</body>
</html>
Here ya go! Just use a function!
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php title();?></title>
</head>
<body>
<?php
function title(){
$monkey = "I like banana!";
echo $monkey;
}
title();
?>
</body>
</html>
Why not just set the variable before you start emitting the page?
<?php $monkey = "I like bananas"; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $monkey;?></title>
</head>
<body>
<?php
echo $monkey;
?>
</body>
</html>
I have a form in which some I have defined some variables in PHP, The question is I need these variables details inside an html which is inside an echo . I don't know what to do as I have tried it.
<?php $var= "LOVE" ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo "<!DOCTYPE html>
<html>
<head>
<title>Details</title>
</head>
<body>
<div > <h1> echo $var ;</h1></div>
</body>
</html>";?>
</body>
</html>
You just need to write the variable on double quote. Here an example
<?php
echo "<html>$var</html>"
// OR if one quote
echo '<html>'.$var.'</html>';
?>
<?php $var= "LOVE"; ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo "
<!DOCTYPE html>
<html>
<head>
<title>Details</title>
</head>
<body>
<div >
<h1>".$var."</h1>
</div>
</body>
</html>";?>
</body>
</html>
There is no need of adding <html>,<body> tags inside another. There is already one above in your code. I do not know why you have used this pattern.
I have the following code snippet in php
if($userName==$dbUserName&&md5($passWord)==$dbPassWord){
echo "<input name='username' type='hidden' value='$userName'>";
header('Location: http://localhost:8080/ClientModule/student.jsp');
die();
}
the php redirects to the following jsp
<%#page contentType="text/html" pageEncoding="UTF-8" errorPage="error.jsp"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Home</title>
</head>
<body>
<h1>
Logged in as: ${param.username}
</h1>
<nav>
<p>Home</p>
<p>Profile</p>
<p>Teachers</p>
<p>Notifications</p>
</nav>
</body>
</html>
I must have done something wrong in the php file, can someone help me spot it?
Any help is much appreciated
When you do this:
header('Location: http://localhost:8080/ClientModule/student.jsp');
The browser simply does a GET request to the specified URL. The form field you echo out on the line above is not included in this request. In stead, what you want is something like this:
header('Location: http://localhost:8080/ClientModule/student.jsp?username='.$userName);