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

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

Related

Combining 2 PHP scripts

I have 2 web pages. Both have PHP scripts. Playing.php has a table showing the last 20 songs played on my shoutcast server. I'm trying to get that displayed on my index page.
Index.html: http://www.deamon.org
The page I'm trying to add is
http://www.deamon.org/scxml/playing.php
Can I do this with include function and echo or is there a better way.
The quickest and easiest way is to use an iframe:
<iframe src="http://www.deamon.org/scxml/playing.php"></iframe>
My best suggestion would simply be to include it with a simple include statement or using an iframe
<?php include("link to file"); ?>
<iframe src="link to file"></iframe>
You could use a PHP include you can do this by adding this line of code into the index file.
First you would have rename index.html to index.php and then place the following code in-between the tags where you would like the page displayed.
<?php include '/scxml/playing.php'; ?>
My suggestion would be to include the .php file into your index page. However including PHP code into an HTML type page requires reconfiguring your server. Instead, do the following:
Change your index.html file to an index.php file. It will be read and rendered the same way without requiring any changes. (make sure that the index.html file doesn't exist on the server so that the index.php file automatically gets picked up
add the following line of code into your index.php file:
<?php include('path/to/playing.php'); ?>

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

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.

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.

is $_SESSION variable is available only to php file and not to html files?

<?php session_start(); ?>
<html>
<head>
<title>
</title>
</head>
<body>
<?php
if($_SESSION['status']=='loggedin'){
echo $_SESSION['name'];
}
?>
</body>
</html>
I started a session for keeping track of logged in users. The same code I put into a profile.html file and tried to redirect to this page if a log in attempt is successful, but I couldn't fetch any information from $_SESSION variables into that file, but when I changed the file extension to profile.php it worked nicely.
PHP scripts are by default only interpreted in .php files (unless you tell the PHP interpreter to do otherwise, e.g., using an .htaccess file). So, you can't use $_SESSION variables in an .html file.
You can write php code only in php file and html in html file or php file.

Get same PHP session ID for both html and php files

I have two files:
html
php
where html file is calling the php file to do something.
Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.
I would like to know how would I do so so that both html file and php file have the same PHP session ID.
Thanks for your help in advance.
You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.
As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.
To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.
EDIT
To answer your qs in comments below -
Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.
Myfile.php
<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
Go to next page (<?php echo $_GET['next_page']; ?>)
......
</body>
</html
so you just open and close php tags where you needed php stuff to go.
To print session ID - just use somewhere in the HTML of PHP file.
Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.
If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:
# Add this to public_html/.htaccess file
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
You should be able to reload the html page and your PHP code will run great.

Categories