php sessions not being detected - php

I have a login page that I register two sessions username and password. then redirect to another page. Once at this page
$_SESSION['username'] = "";
$_SESSION['password'] = "";
after login check I have the next page check if the session is registered
session_start();
if(isset($_SESSION["username"]){
continue
}else go back to login page
Once I'm logged in I want to go to another page that depending on if the session variable is set I display something different on the page.
So on the galery page I do
at the very top of page I do
<?php
session_start();
?>
then further down where I want the button to be I do
<?php
if (isset($_SESSION['username'])){
show a new button
}
?>
I get the button to show but at the top of the page I have
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent
and it messes up how my page is displayed. Any ideas? I have the session_start(); at the very begging of page I don't know why this is happening. Any ideas?

You'll get that error if anything outputs to the browser before you call session_start(). For example, you can't do:
<?php
echo "Test";
session_start();
You also can't do:
<?php session_start();
(note the space before the <?php)
Make sure nothing - no HTML, no blank lines, no spaces - is written out prior to your session_start() calls and you'll be fine.

You need to ensure there's nothing (whitespace, UTF-8 BOM) before your <?php. This also applies to any files you include before the session_start() call.

Is the gallery page being included in another file?
<?php
// lots of php code
include('/path/to/gallery.php');
?>
If anything is being sent to the browser before the session_start(); it will create this error.

Session is a header, headers can't be sent after any output (even a single space). You got 2 options, place session_start() before ANY output or you could also use output buffering, this allows you to send headers after output.
Place this at the top of your script
ob_start();
And this at the end
echo ob_get_clean();

Related

Warning: session_start(): Cannot send session cache limiter, even though I am checking for existing session

I am having trouble with my sessions.
I get the error
Warning: session_start(): Cannot send session cache limiter
I have this code in 2 pages
if (!isset($_SESSION)) {
ini_set('session.gc_maxlifetime', 3*24*60*60);
session_set_cookie_params(3*24*60*60);
session_start();
}
I can only find instances of people who haven't does the isset check, so I have no idea why this is happening :(
I've checked for whitespace, my php tag is on line 1 in both files.
This is the first page, which loads dbmgmt which has the above code. I need the session code in both files because dbmgt is not always included from a page that creates a session.
<?php
if (!isset($_SESSION)) {
ini_set('session.gc_maxlifetime', 3*24*60*60);
session_set_cookie_params(3*24*60*60);
session_start();
}
require("dbmgmt.php");
?>
You are trying to call session_start(), which is trying to send an HTTP Header to the output of the page.
But somewhere earlier in your script, you've already started outputting the content of the page. The headers must come before the content.
Try calling this code before any HTML or content (even spaces or whitespace outside of the ?<php and ?> tags).
TRY
<?php
session_start();
ob_start();
?>
This will solve your issue.

session variables not working across different pages

Once i logged in i set a session variable inside body element(inside login.php) as below:
session_start();
$_SESSION['Username'] = $_POST["Username"];
if(isset($_SESSION['Username']))
$loginTrue = 1;
else
$loginTrue = 0;
and on top every page i have added this
<?php
session_start(); //this was added after seeing many suggestions in stack overflow that session_start() has to be called at the top on each page. Though i tot calling once was sufficient.
if(isset($_SESSION['Username']))
$loginTrue = 1;
else
$loginTrue = 0;
?>
Now whenever i redirect my page after login from login.php $_SESSION['Username'] gets unset, i dont know how. I redirect using a button click as in
onclick execute window.location = home.php
This is not comman
check your code with this code may be some error is on the page
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
?>
And check your php.ini setting for this.
And check your code with different browser.
And any blank output should be on your page before session start.
alright guys i solved it somehow. I dont know how i did but i did. Firstly i created 4 webpages, a small one just to check the if session variables are supported. Once i confirmed this then i did the below and my original webpage started working session start is very important at top of all pages. Also try session activity in different browsers. Also check by closing dreamweaver. Also make sure is used instead of transitional and stuff and also that session start comes before doctype html declaration

Redirect to a Order Success Page once the form has been submitted succesfully

I have a form which is supposed to be redirected to order success page once the submit button is clicked.
I tried to use hearder() but headers already sent error came. No way I could put this at the top of the php file as I have to redirect to the old order page if the submitted form has errors.
Is there any other way of doing it?
One more doubt,
Currently I am using the following to clear all the sessions!
<?php session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true); ?>
So do I have to use this <?php session_start(); ?> again in the form if I intend to use the session in the same page after the above give n code?
You could do a couple of things,
Use javascript to redirect the user by outputting <script type="text/javascript">parent.location.href = 'nextpage.php';</script>
You could use Output Buffering to send a HTTP Header after outputting.
While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
You could add a meta refresh header <meta http-equiv="refresh" content="0;url=nextPage.php">
You can show the user a link to the next page and let him click on it himself.
You are sending headers by content being sent to the browser, in other words something has been echoed before you tried to header();
Find out what, exit; before you want to header(); to try find out the issue
I would never normally suggest this, because it would be EXTREMELY bad practice, but to force it:
ob_clean(); // Cleans the output buffer
header("Location: redirect-url");
To clear sessions:
$_SESSION = array();
session_destroy();
Update:
Yes you must use session_start(); again to re-initiate a NEW session, but remember you destroyed the old one so the data is cleared - A new session = empty session

Session ID and variables in PHP

I have a login page where once a user is logged in successfully it echos a link to their personal page. When that page loads I want it to check if the user has access to it so someone doesn't try to just type in www.mywebsite.com/bob.php in the url. I tried to use a cookie to send the user info but I realized you can't use cookies after html has been written to the page. Does anyone know an efficient way to do this that is also fairly simple? Thanks
After the user logs in, assign his id to a session variable:
<?php
session_start();
$_SESSION["userid"] = $userid;
?>
On the protected page, check if the user has a $_SESSION["userid"] variable set:
<?php
session_start();
if (isset($_SESSION["userid"])) {
//show page
}else{
echo "No rights";
}
?>
It is true that you cannot set cookies when output has already been sent to the browser. A useful trick is to use output buffering. Basically, you begin your code with a call to ob_start() and end it with ob_end_flush(). Now you can set cookies (and any HTML header) wherever you want in your code.

Sessions not working PHP

My website doesn't start a session when I visit, I don't know why but my website works like this:
<?php
session_start();
$title = "Home";
include("include/header.php");
include("include/functions.php");
?>
...HTML stuff here...
<?php
include("footer.php");
?>
But when I check with Cookies (add-on for Firefox) there are no sessions started... I used session_regenerate_id(); but it doesn't work at all.
It fails to log in since there are no sessions, I do not have any session_destroy() in my website, only in the logout.
But funny thing is, when I login (without refreshing or navigating just yet) and then click on the logout button, there is a session on my website, then when I log in again, it tells me that I am logged in BUT if I login and navigate or refresh, it doesn't tell me that I'm logged in since there are no sessions...
Logout:
<?php
session_start();
session_destroy();
setcookie("cookie-name", "", time()-60, "", "", 0);
header("Location: ../index.php");
exit;
?>
What do I do?
You must have session_start() at the beginning of every file that is being accessed and uses sessions. The name is misleading, session_start() actually doesn't start a new session but initialzes PHP session menagment.
Not sure if it's related, but there was a strange PHP quirk that required the SESSION_START() to be on the line immediately below the <?php tag. Something about whitespace and extra things above the session used to make it go haywire for me. I've been using Zend of late, which avoids that issue with its own session handling system.
You might try doing a print_r($_SESSION) to see if there's anything in the session array at all.
It's probably because you are not setting a session in either of the examples you have given, you have to have a line like the one below to actually create a session, and then to access the session variables on all subsequent pages you need session_start();
$_SESSION['example'] = 'something';
It doesn't look like your setting anything in the session or the cookie.
If you want to pass information around in the session you'll need to assign the necessary values in the $_SESSION variable.
For example on your main page you can do:
<?php
session_start();
$_SESSION['myVariable'] = "my text";
?>
And then on any subsequent pages you can access the variable you've set.
<?php
session_start();
echo $_SESSION['myVariable']; //This will print "my text"
?>

Categories