Unable to setcookie() when SSI-include a PHP header file - php

Really lost here on what to do next. Lets say, I have
header.php
<?php
setcookie("the_cookie","data",time()+60);
?>
//followed by HTML codes
index.htm
<!--#include file="header.php"-->
<html>
//standard html stuff here
</html>
When I include header.php in any of my html file, it fails to set cookie. This is despite
<!--#include file="header.php"-->
it being the very first line on my html document and setcookie being the very first line of the header.php The Apache server is SSI enabled. So I am certain the #include works as I have other HTML codes after the setcookie() function , and it shows correctly.
But when I run header.php itself, the cookie is set correctly. Has anyone here has ran into such situations before and knows what needs to be done?
Thank you in advanced
Gary Cho

I had the same problem, I solved it by using $_SESSION[] instead of setcookie(). Another benefit is that you can use also echo $_SESSION[] in the same php run that you set your $_SESSION[] value. I hope it works for you as well.

Related

Using PHP Virtual() to include another PHP file fails

I have HTML and PHP files that include the "header" HTML for my website. In HTML files I include this header using
<!--#include virtual="/top.ssi" -->.
top.ssi in turn includes other files:
<!--#include virtual="/navbar.ssi" -->
<!--#include virtual="/advertising/slider_advertising.ssi" -->
and slider_advertising.ssi includes:
<!--#include virtual="/advertising/advertising.php" -->
This is the critical file as it prepares advertising data for display.
All the above works great when run from HTML files.
Now I have some PHP-driven webpages (Logon.php) that also want to display the header of the website using top.ssi. This code starts with:
<!DOCTYPE HTML>
<html>
<head><title>Login to HVmusic</title>
<?php
virtual("/top.ssi");
...
Here is where the problem comes in. The virtual("/top.ssi"); executes OK, but stops executing after it encounters the PHP file that is included in top.ssi <!--#include virtual="/advertising/advertising.php" -->. The output from advertising.php is displayed and then the PHP output from logon.php stops. So the output stops without even displaying the entire header.
If I remove the statement for <!--#include virtual="/advertising/advertising.php" --> then the logon.php displays it's page normally (without, or course, that bit of the header displayed by advertising.php). So this tells me that PHP is having a problem with a virtual() file that includes another PHP file.
Is there any way to fix this? Is it some known restriction in PHP? I've been googling for two days and can find no mention of this issue. Thanks for any help you can offer.
Thanks for the tips, that clarified my thinking. I have just converted all my web pages to PHP and this solved the issue. Most of my pages were already PHP, so it wasn't much work to convert the HTML pages. All the include files I used were easily converted to use <? php require "filename"; ?> instead of <!--#include virtual="filename" -->
The underlying problem probably has to do with some conflict about running in PHP, then calling virtual() which calls an apache instance, which then calls another PHP instance to process the PHP include files I used.

Where exactly do I put a SESSION_START? [duplicate]

This question already has answers here:
When do I have to declare session_start();?
(2 answers)
Closed 9 years ago.
So I'm starting my own website and I have the login file pretty much made. I just need to figure out where to put the session_start to keep the user logged in. Where exactly do I put the session_start? Do I put it right in the login file? Or where do I put it?
Thanks for the help
Put it after your PHP start tag <?php ... like this
<?php
session_start();
//... your code....
//more code....
Read more on sessions from the PHP Manual. Here
Note : Also keep in mind, you need to call session_start(); on each and every page if you are making use of session variables.
Put it right after the start tag, or else headers will have been send, and the session, AFAIK, has to be the first header sent
<?php
session_start();
//session code here
?>
Right after <?php tag.
Be sure that there is NO output before this function (even a space symbol or so).
You want to put session_start(); at the top of your page before any other code. However, if you are using includes to make your life easier, it's best to put it at the very top of a file that is included in all files. For instance, when I make a website, I put all of my header code and footer code in separate files and include them in the other files. I also have a functions file that is included in every other page of the website. So for my index file, it may look something like this:
<?php include_once("includes/header.php"); ?>
<div id="content">
Website Content
</div>
<? include_once("includes/footer.php"); ?>
Then, my header file would start like:
<?php include_once("includes/functions.php"); ?>
<!doctype html>
<html>
<body>
Then at the top of my functions file:
<?php session_start();
[functions]
?>
In this way, the functions files' code gets ran first, therefore the session start code is the very first thing hit. Why? You cannot have any type of output to the browser before starting a session.
it's better to have a separate file other than your login to do some common stuffs.
i think your login file will be generally handling user verification and validation thing. so don't include that file on every page.
have one more file that
includes all required files
keeps all your analytic scripts
initializes global variables
and this file you can start with <?php session_start(); ?>
session_start() needs to go in every page/file that refers to $_SESSION (obviously the login page is included).
Because you should only be calling it once, I tend to write a lazy_session_start() method (and tend to put it in an include file):
/**
* Lazily calls session_start (to prevent warnings).
*/
function lazy_session_start() {
if (!isset($_SESSION) || !is_array($_SESSION)) {
session_start();
}
}
It could be called like so (before you need to use $_SESSION):
<?php
//you must either declare "lazy_session_start" function
//or import the file containing the function definition.
require_once('lazy_session_start.php'); //or something.
lazy_session_start();
//... you may now use the $_SESSION array.

Php Include function causes whitespace and ignores style

I have a page that uses another page via the include() function. The page that requests and uses this external page has some html embeded as well as some output using the echo function. Here is an example of what I am referring to.
page Apple.php contains:
<html>
<head></head>
<body>
<h1>I have some content here.</h1>
<div id="format" style=" width:20px; height:20px;"><?php include("Orange.php") ?></div>
<h2>Some more content here.</h2>
</body>
<html>
page Orange.php contains:
<?php
$astringA='blue';
$astringB='berry';
include("someExternalPage.php");
echo "fruit is ".$astringA.$astringB.$externalVariable;
?>
Whenever the Apple.php is viewed in browser, only the top part of its body is shown(the part above the include function) and everything below the div box is lost and filled with whitespace. In addition the included file ignores all style that the div box has. The same is true for the Orange.php; all output below the include() function is lost. How can I fix this so that the included file will conform to the style I want for it and also to continue to output the rest of the content on the pages?
I also notice that even if I try outputing the rest of the page through php such as
echo ">h2>Some more content here."; (I deliberately inverted the first ">" in the line above so that I illustrate the code here)
the included still takes precedence over the current page and thus the output is never shown.
Smells like you have a hidden fatal error here.
Try using error_reporting(E_ALL) and run the page to see if you have an error...
There's something wrong either with someExternalPage.php or $externalVariable.
If someExternalPage.php is actually an external file (ie you are including a file such as http://example.com/test.php) you may need to check that you have enabled allow_url_include in your php.ini
Alternatively, please make sure someExternalPage.php exists.
Looks like you're missing a semicolon after the include in Apple.php. Can we see the output when you try to load Orange.php? It's helpful to see exactly where the flow is breaking.

_SESSION['xxx'] not being retained across pages in my web application

I have an html file which has inline php. The index.html looks roughly as follows
<?php session_start(); echo $_SESSION['xxx']; ?>
<form action=blah.php> ... </form>
And in blah.php, I do a
$_SESSION['xxx'] = "foo";
header('Location: index.html');
However, when index.html is shown for the second time, I do not see the "foo" message.
Then check your server settings. Try to set send session values in address line (like GET request).
Run this bit of code before your starting your session. It's possible there is an error accessing the session file
ini_set("display_errors", "stdout"); error_reporting(E_ALL);
PHP cannot parsed by HTML files, I mean PHP codes cannot run by HTML, you can try like
<!--#include FILE="test.inc" -->
Above code is HTML file include and put your PHP codes in .inc file, but you have to make some changes in Apache

How can I redirect using a PHP script called from an SSI?

On a WAMP server, I have a server-side include in a file, a.shtml, composed of the following code:
<!--#include virtual="./req.php"-->
The contents of req.php are:
<?php
Header("Location:index.php");
echo "still here";
?>
When I open a.shtml, I see the text still here, but the page has made no attempt to redirect itself. Why is this? And is there any way to make it work?
Thanks for the help
EDIT: The reason I want to do this is because I have some session variables that I want to influence the way the PHP script acts. If the session variables are not set, I need it to redirect to a login page. I know I can just write the entire thing in PHP, but I'd like to do it this way if possible. If it's not possible to change header information from an included PHP file from SSI, then I'll just do it entirely in PHP.
it's impossible
you don't need that.
just address tour script that set session variables directly, not through ssi
MAYBE (with capital letters Lol), you can pull this off if you call that script in an IFRAME and that IFRAME outputs some JScript like window.parent.location = <some_url_here> forcing its parent to change its location... Its just fast-thinking from my part, I might be wrong with IFRAMEs' parent-child relation to the original document, as I haven't tested the "idea" myself :)
If your req.php returns the following html code, the redirect will happen:
<html><head>
<title>HTTP 301 This page has been moved</title>
<meta http-equiv="Refresh" content="0;URL=https://www.example.com/index.php">
</head>
<body></body></html>
But: "Google Warning: Using The Meta Refresh Tag Is Bad Practice"

Categories