PHP header conflict [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 5 years ago.
Hello i make a project and i have the fallowing problem.
In my index page i have a dynamic layout:
<html>
<head>
<link rel="stylesheet" type="text/css" href="index2.css">
</head>
<body class="news">
<header>
<div class="nav">
<ul>
<li class="home">Home</li>
<li class="logare"><a class="active" a href="index2.php?page=admin_login">Admin</a></li>
<li class="registration">Registration</li>
<li class="produse">Produse</li>
</ul>
</div>
</header>
<div id="content">
<?php
$p=$_GET['page'];
switch($p){
case "welcome":
include('welcome.php');
break;
case "admin_login":
include('Admin_login.php');
break;
case "registration":
include('Registration.php');
break;
case "produse":
include('produse.php');
break;
default:
include('welcome.php');
break;
}?>
</div>
</body>
</html>
in default and welcome case it;s redirecting me to my welcome.php page:
<?php
session_start();
if(!$_SESSION['email'])
{
header("Location: login.php");//redirect to login page to secure the welcome page without login access.
}
?>
<html>
<head>
<title>
Registration
</title>
</head>
<body>
<h1>Welcome</h1><br>
<?php
echo $_SESSION['email'];
?>
<h1>Logout here </h1>
</body>
</html>
Because i have a redirecting in welcome.php too ,my header is not loading from the dynamic layout.How can i repair this,but still redirecting me to login?

To avoid PHP header conflicts, I prefer to use JS redirection instead. See the below code that I have modified for you.
<?php
session_start();
if(!$_SESSION['email']) {
?>
<script language="javascript" type="text/javascript">
alert("User not logged in.");
window.location = 'login.php';
</script>
<?php
}
?>
Hope this helps.

Related

Parse error: syntax error, unexpected end of file in php on line 48 [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
afternoon\am having difficulties in determining the error of the code and i would need assistance. each time i execute the code am given an error in which i am unable to determine the error itself
<?php
include('functions.php');
if (!isLoggedIn()) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Home Page</h2>
</div>
<div class="content">
<!-- notification message -->
<?php if (isset($_SESSION['success'])) : ?>
<div class="error success" >
<h3>
<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>
</h3>
</div>
<?php endif ?>
<!-- logged in user information -->
<div class="profile_info">
<img src="images/user_profile.png" >
<div>
<?php if (isset($_SESSION['user'])) : ?>
<strong><?php echo $_SESSION['user']['username']; ?></strong>
<small>
<i style="color: #888;">(<?php echo ucfirst($_SESSION['user']['user_type']); ?>)</i>
<br>
logout
</small>
<?php endif ?>
</div>
</div>
</div>
</body>
</html>
You are missing a closing bracket in the first lines of your code :
<?php
include('functions.php');
if (!isLoggedIn()) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
} // <- Here is the mistake
?>

PHP/HTML structural way

I have a sample index.php page like this:
<?php
Define("_DEF",1);
require_once "database.php";
require_once "session.php";
require_once 'functions.php';
if (isset($_GET['logout'])) {
$session->logout();
} else if (isset($_GET['add'])) {
$addFlag = 1;
}
if(!$session->is_logged_in())
redirect_to("login.php");
?>
<html>
<header>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
</header>
<head>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<title>Messaging Panel</title>
<link href="style_index.css" media="screen" rel="stylesheet" type="text/css" />
<link href="iconic.css" media="screen" rel="stylesheet" type="text/css" />
<script src="prefix-free.js"></script>
</head>
<body>
<div class="wrap">
<nav>
<ul class="menu">
<li><span class="iconic home"></span> Home</li>
<li><span class="iconic plus-alt"></span> New Message</li>
<li><span class="iconic mail"></span> List Messages</li>
<li><span class="iconic user"></span> Logout</li>
</ul>
<div class="clearfix"></div>
</nav>
</div>
<?php if (isset($addFlag) && $addFlag==1){ ?>
<h3>Add Message HTML Goes Here!</h3>
<?php }
</body>
</html>
I have several HTML forms for different action. E.g. when user calls index.php?add I want to display a custom form to add new message into my database. My current code as you can seed will be so complex if I'm goint to have several actions in my index.php and it will looks like a big if-else structure which be hard to debug. I want to know are there any structured method to have specific HTML for each situation and then include it based on PHP variables status? Currently I know I can call a function to display the form using echo HTML_TAGs , but are there any methods to have HTML form in seperate file and php functions bring it to HTML based on passed variables?
My main target is to have a structure of files for each situation (one for add record, one for list records , ...).
Note: Currently I don't know anything about jQuery. So I look for simple HTML+PHP solutions! :)
You can use require_once,require,include and include_once
Sample form
addForm.php
<form action="tosend.php">
<!-- Sample elements here -->
<input type="text" name=""/>
</form>
then in your php file where you want to include html file just use require_once or include_once
Sample
if(condition){
//condition is meet include the form
include_once('location/addForm.php');
}else{
include_once('includeAnotherForm.php');
}
You can have HTML elements in different files. Than you can require('file.ext') to display that page.
<?php
require(__DIR__.'/header.php');
if (isset($_REQUEST['page']) && file_exists(__DIR__."/pages/{$_REQUEST['page']}")) {
require(__DIR__."/pages/{$_REQUEST['page']}.php");
} else {
require(__DIR__."/pages/home.php");
}
require(__DIR__.'footer.php');
?>
Personally, since you already have separate files for session.php, etc. I'd probably write a view.php as well, with the following setup:
function view($name)
{
$parts = explode('.', $name);
$template = '/views/' . implode('/', $parts) . '.php';
require_once __DIR__ . $template;
}
Then you can use it like so, storing all your view files in the ./views directory:
require_once './view.php';
view('home.message'); // {path}.{to}.{file} => /{path}/{to}/{file}.php
You can also write it as a class if you so wish.
PS - this code above was not tested, but the principle is sound and is aimed at creating a view file like this:
<!DOCTYPE html>
<html>
<head>
<?php view('layout.head'); ?>
</head>
<body>
<div class="wrap">
<?php view('layout.menu'); ?>
</div>
<!-- load conditionally -->
<?php if (isset($addFlag) && $addFlag == 1) view('add.message'); ?>
<?php view('layout.footer') ?>
</body>
</html>
I'd should simple enough to use the INCLUDE statement.

Error with this Modify Header [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
Just to give you a short background, I am working with a referral program and the main feature of it is referring someone.
In my website. I have registration page, login page, successful registration page, referral page and referral list.
For example, if you are a user, you have to register through registration page. If you successfully register on my page, the next page will be the successful registration page. There is a button there to go to the referral page to refer someone.
The problem I encounter is, I register as a new user. So definitely, I filled up all necessary information to registration page, after I successfully register, the next page after that is showing the successful registration page and in that page there is a button that once you click it, it will go to referral form page.
For example, I will now refer someone, so I filled up all information, so when I tried to submit my referral there is an error that prompt at my page
Can anyone tell me which line of my code is triggering this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/thecommissioner/public_html/crm/include/Webservices/Utils.php:880)
in
/home/thecommissioner/public_html/crm/modules/Webforms/capture_old.php
on line 97
but the referral added on my referral list, so when I tried to refer again, this error won't show anymore... even if I refer several times, this error didn't show anymore. It just shows on the first referral try of a new created account. I don't know what the problem is.
This just shows on my first try to referring somebody, but after that, when I tried to refer more, this wont show anymore and I already added my referral. Can someone help me to figure this out?
I know there are several lessons there and I've already read different posts in stackoverflow about this and I'm still vague about it. And would you mind telling me what new code I can use instead of my current code? I cannot test if my page is working because of this.
Here's my code:
<!DOCTYPE html>
<?php
include('../include/dbconnection.php');
include('../include/functions.php');
if(!isset($_SESSION))
{
session_start();
}
$empid = $_SESSION['SESS_EMP_ID'];
$conid = $_SESSION['SESS_CONID'];
$fName = $_SESSION['SESS_FIRSTNAME'];
$lName = $_SESSION['SESS_LASTNAME'];
$contactNo = $_SESSION['SESS_CONTACT_NO'];
$mobile = $_SESSION['SESS_MOBILE'];
$email = $_SESSION['SESS_EMAIL'];
$bday = $_SESSION['SESS_BDAY'];
if($conid == '')
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='index.php';
</SCRIPT>");
}
else
{
//Nothing
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/skeleton.css">
<link rel="stylesheet" href="../css/successReg_style.css">
<link rel="icon" type="image/png" href="../images/cvglogo.png">
<link rel="short icon" href="../images/favicon.ico">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<link rel="stylesheet" href="../css/animate.css">
</head>
<body>
<!--Here the Header goes-->
<?php include('include/logoheader.php'); ?>
<!-- Overall Contect for Responsive Purposes-->
<div class="allcontent">
<div class="row">
<div class="twelve columns" style="margin-top:0%;">
<!-- Here the Main Content goes-->
<div="maincontent">
<center>
<h3>Congratulations and Welcome to the Circle!</h3>
<br>
<div class=" animated zoomIn socialmedia">
<ul>
<li>
<img class="logos" src="../images/tick.png">
</li>
</ul>
</div>
<!-- This is the welcome Introduction -->
<div class="welcome">
<p>Now that you’re part of the most awesome group on the planet, why not share the glory with your friends. There’s no happier
team
player than a team player with his friends.</p>
<span class="hit">Don’t be shy. Hit the button. Refer your friend!</span><br>
<!-- This is Just a GIF arrow down -->
<div class="demo-wrapper">
<div class="html5-dialog">
<div class="gif">
<img src="../images/scroll-down.gif">
</div>
<?php include('GlobalConstant.php'); ?>
<!-- But here goes the button for "I have someone in mind" -->
<a href="<?php echo employee_refer; ?>"><button class="navbutton">I have someone in mind</button>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Here the footer goes-->
<center><?php include("include/footer.php"); ?></center>
</body>
</html>
The error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/thecommissioner/public_html/crm/include/Webservices/Utils.php:880)
in
/home/thecommissioner/public_html/crm/modules/Webforms/capture_old.php
on line 97
just shows on my first attempt of referring then after that, when I referred more, the above warning/error didn't show again.
If those warning just shows on my first attempt of referring, why it doesn't show the next time I referring again? It just shows on my first try.
You need to send header before ANY output is sent. That means before that HTML, on top of page.
OR, as RamRider suggested, use output buffering.
Put
ob_start()
On very top of page and
ob_end_clean()
On very bottom
You need to put the session_start() function on top your code. Otherwise sometimes php will throw the exception headers already sent.
<?php
session_start();
include('../include/dbconnection.php');
include('../include/functions.php');
$empid = $_SESSION['SESS_EMP_ID'];
$conid = $_SESSION['SESS_CONID'];
$fName = $_SESSION['SESS_FIRSTNAME'];
$lName = $_SESSION['SESS_LASTNAME'];
$contactNo = $_SESSION['SESS_CONTACT_NO'];
$mobile = $_SESSION['SESS_MOBILE'];
$email = $_SESSION['SESS_EMAIL'];
$bday = $_SESSION['SESS_BDAY'];
if($conid == '')
{
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.location.href='index.php';
</SCRIPT>");
}
else
{
//Nothing
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/skeleton.css">
<link rel="stylesheet" href="../css/successReg_style.css">
<link rel="icon" type="image/png" href="../images/cvglogo.png">
<link rel="short icon" href="../images/favicon.ico">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<link rel="stylesheet" href="../css/animate.css">
</head>
<body>
<!--Here the Header goes-->
<?php include('include/logoheader.php'); ?>
<!-- Overall Contect for Responsive Purposes-->
<div class="allcontent">
<div class="row">
<div class="twelve columns" style="margin-top:0%;">
<!-- Here the Main Content goes-->
<div="maincontent">
<center>
<h3>Congratulations and Welcome to the Circle!</h3>
<br>
<div class=" animated zoomIn socialmedia">
<ul>
<li>
<img class="logos" src="../images/tick.png">
</li>
</ul>
</div>
<!-- This is the welcome Introduction -->
<div class="welcome">
<p>Now that you’re part of the most awesome group on the planet, why not share the glory with your friends. There’s no happier
team
player than a team player with his friends.</p>
<span class="hit">Don’t be shy. Hit the button. Refer your friend!</span><br>
<!-- This is Just a GIF arrow down -->
<div class="demo-wrapper">
<div class="html5-dialog">
<div class="gif">
<img src="../images/scroll-down.gif">
</div>
<?php include('GlobalConstant.php'); ?>
<!-- But here goes the button for "I have someone in mind" -->
<a href="<?php echo employee_refer; ?>"><button class="navbutton">I have someone in mind</button>
</a>
</div>
</div>
</div>
</div>
</div>
<!-- Here the footer goes-->
<center><?php include("include/footer.php"); ?></center>
</body>
</html>
If you are still experiencing the issue try ob_start() and ob_flush() .. Anyways this stackoverflow article about headers already sent is worth reading though Stackoverflow link

navigation include loop(php switch)

I am somewhere in a loop! My browser is including over and over again my navigation until the page crashes. I dont know why. I do the switch normally in the index.php, but I want to have an intro page and therfore I want to do it in the home.php.
Here is a pic of my folder structur:
Thats my index.php:
<body onload="main()">
<!-- Main content -->
<div id="main">
<!-- Logo -->
<div id="logo">
</div><!-- End Logo -->
<!-- h2 Webdesign & Development -->
<h1 id="text"></h1>
<!-- End h2 Webdesign & Development -->
<!-- Enter link -->
<div id="enter">
ENTER
</div><!-- End enter link -->
</div><!-- End main content -->
There is a "ENTER" Link to the real website(home.php)
Here is my navi.inc.php file:
<nav>
<ul>
<li><img data-other-src="../pics/home_button_hover.svg" src="../pics/home_button.svg" id="home" alt="Home Button"></li>
<li>Info</li>
<li>Projects</li>
<li>Contact</li>
</ul>
and finally my home.php:
<?php
require_once '../include/config.inc.php';
if( isset( $_GET['site'] ) ){
$site = $_GET['site'];
}else{
$site = 'Home';
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Home | <?=$seiten_name?></title>
<link rel="stylesheet" type="text/css" href="../style/main.css">
<link href='http://fonts.googleapis.com/css?family=Iceland' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../js/image_switch.js"></script>
</head>
<body>
<div id="main">
<?php
// Navigation
switch ( $site ) {
case 'Home':
include('home.php');
break;
case 'Projects':
include('projects.php');
break;
case 'Contact':
include('contact.php');
break;
default:
include('home.php');
break;
}
?>
<div id="preloader">
<img src="../pics/ajax-loader.gif" alt="Preloader">
</div>
</div>
<?php
require('../include/navi.inc.php');
?>
</body>
</html>
Sorry for my poor english, but i hope you will understand my problem!! I want the home.php as my index :P
In your home.php file you have the following code:
switch ( $site ) {
case 'Home':
include('home.php');
break;
(Same goes for your default: block)
Which basically means that there's a situation where the home.php file will include itself, which will cause an infinity loop.

Redirect button in php [duplicate]

This question already has answers here:
What does "href" stand for? [duplicate]
(5 answers)
Closed 7 years ago.
Hello guys I want to redirect a user to another page when clicking on a button on my webpage, but I dont know how to do it. My buttons are "home", "coffee", "shop" and "about us". When clicking on any of these, I want to get to another page. Can anyone help please?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="Styles/stylesheet.css" />
</head>
<body>
<div id="wrapper">
<div id="banner">
</div>
<nav id="navigation">
<ul id="nav">
<li>Home</li>
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>
</ul>
</nav>
<div id="content_area">
<?php echo $content; ?>
</div>
<div id="sidebar">
</div>
<footer>
<p>All rights reserved</p>
</footer>
</div>
</body>
</html>
and this is my index code:
<?php
$title = "Coffee Webpage";
$content = '<img src="https://encrypted-tbn2.gstatic.com/images? q=tbn:ANd9GcSmRPsVdzF4BYRWbF3yBpbkW-1ZjyURYWX1JQm0e6RAbJPR0H76" class="imgLeft" />
<h3>Title 1</h3>
<p>
</p>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdJv4yTpue170rTnOn9unKMgYWeOSPxBP9LoBwvUvhcDJNFcPG6w" class="imgRight"/>
<h3>Title 2</h3>
<p>
</p>
<img src="http://seattle.eat24hours.com/files/cuisines/v4/coffee.jpg?e24v=225" class="imgLeft" />
<h3>Title 3</h3>
<p>
</p>';
include 'Template.php';
?>
add the url to that page in to your link so for example if the page coffes is coffe.php then put coffe.php in there.
alternately put your full url which I would recommend.
you could also add this before your link to get the site url using php
<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ?>
Typically, you'd want to do this by adding a front controller; or, in your case, turning your index.php into a front controller.
<?php
$page = $_GET['page'];
switch($page) {
case 'coffee':
// set variables
break;
case 'another':
// set variables to something else
break;
}
require 'template.php';
Put a link to each page in their corrosponding a href attributes.
Example:
<li>Coffee</li>
<li>Shop</li>
<li>About us</li>

Categories