i have the following code:
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
echo $_SESSION['views']; //retrieve data
i tried to break it up in 2 parts like this:
//Page 1
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
.
//page 2
echo $_SESSION['views']; //retrieve data
it echos nothing, what am I doing wrong?
as Gumbo mentioned, you need to call session_start() on every page you want to use the session..
You also mentioned you were getting the error:
Warning: session_start(): Cannot send session cache limiter - headers already sent
This is due to data being output on the page before the session_start() is being called, you have to include this before anything is echo'd to the browser.
Make sure to call session_start on every page you want the session to be available. ob_start is not the session handler but the output buffer handler.
session_start() in 2 files before any output.
Related
I want to access $_SESSION['roleid'] in master.php. master.php is included in every page. I'm only able to access $_SESSION['roleid'] in dashboard.php after user login. How to access $_SESSION['roleid'] in every page.
<?php
session_start();
if($_SESSION['login']==1) {
$_SESSION['loggedIn'] = true;
$role_id1 = $_GET['role_id'];
// store here in session
$name=$_GET['name'];
$_SESSION['roleid'] = $role_id1;
// $role_id=$_SESSION['roleid'];
$a=$_SESSION['roleid'];
// echo $a;die;
if(isset($_SESSION["roleid"])){
header("location:api/dashboard.php?role_id=$a?name=$name");
}
} else {
header("location:index.php");
echo "login unsuccessful.";
}
?>
To be able to access the session variables you need to call session_start(); on top of every page that will use the session variable. After the start call has been made you can use session variables like this echo $_SESSION["my_var"]; and this to set the content $_SESSION["my_var"] = "Var content";, if you are unsure what the session actually belongs it is possible to check the content of the session by doing var_dump($_SESSION);. This will show all the data the session contains since it is passed as an array.
Please do remember that a session is not recursive through subdomains because of the cookie that is being used to track which session belongs to who. A session is also dependent on that headers are not sent yet since it needs to interact with the cookies.
To delay sending of headers do this:
1. Call ob_start(); at the completely top of the scripts that needs to set multiple headers
2. Do the things you need to do like set headers and so on
3. Call ob_end_flush(); to send the headers.
Here is the offical PHP docs on this:
https://www.php.net/manual/en/function.ob-start.php
https://www.php.net/manual/en/function.ob-end-flush.php
you should check $_SESSION['roleid']:
* if having $_SESSION['roleid'], you will get it. On that code, you store $_GET['role_id'] to $_SESSION['roleid'] but $_GET['role_id'] have no in all page, it's only in dashboard.
I think that. You should try.
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.
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();
I'm not a PHP developer here.
I have a page that is unable to display session values even though they definitely exist. I am able to view them on another page, yet for some reason they cannot be seen on a certain page!?
EDIT:
Below is the script that exists on the top of the page
<?php
require_once('eu_gl.php'); // <- includes session_start() in it
if(!session_id()) session_start(); // added this in case, but should not be needed
?>
Contents of the include:
<?php
/*** Global include file **/
set_time_limit(300);
$time1 = microtime();
define('APP_SESS_NAME', 'EURA');
session_name(APP_SESS_NAME);
session_start();
session_set_cookie_params(0);
//...
?>
As #k102 mentioned, ensure you have session_start(); somewhere before you set/get your session variables. print_r($_SESSION); can also be handy in showing you what session information exists ...
I personally would modify your code to have this:
if (!isset($_SESSION)) session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
print_r($_SESSION);
First point is :
better try
if(session_id() == "" ) session_start();
instead of
if(!session_id()) session_start();
as session_id wont return false.
php manual:
session_id() returns the session id for the current session or the
empty string ("") if there is no current session (no current session
id exists).
Second point is that session_start shall be the very first thing on your page unless you really need sthg else, but I can't see any reason in your code.
You have to make 2 php file for checking $_SESSION, session used for storing variable so you can use it in all page of your website.
test.php:
set_time_limit(300); // Timeout for script
$time1 = microtime(); // What this variable do in your script
define('APP_SESS_NAME', 'EURA'); // set constant APP_SESS_NAME
session_name(APP_SESS_NAME); // name session APP_SESS_NAME
session_start(); // start session
session_set_cookie_params(0); // this line must be called before session_start();
if(!session_id()) session_start(); // Delete this line
$_SESSION['name']= "test"; // set variable session with params name for checking session
test2.php:
define('APP_SESS_NAME', 'EURA'); // set constant APP_SESS_NAME
session_name(APP_SESS_NAME); // name session APP_SESS_NAME
session_start(); // start session
echo $_SESSION['name']; // check session is valid
I think you should understand about Session now.
Seems the naming of the session was the issue. Tt was named in the include: session_name(APP_SESS_NAME), then it seems I have to use that session name when starting it elsewhere.
For some reason my post id session is not carrying over to my grading script page. I grab the post_id from the posts url in my posts page and turn it into a $_SESSION to carry it to my grading script page. My $_SESSION that holds the logged in users id $_SESSION['user_id'] if logged in carries over to the grading script but not the post $_SESSION. How can I have my posts id carry over to my grading script page?
I have session_start(); at the top of both of my pages.
$_SESSION['post_id'] = $_GET['pid'];
You may have some output before you started the session. If that is the case, the session might not be able to be set correctly. Enable debugging mode and search for an error regarding the session_start() function:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
Or check the error_log if you can't switch debugging on.
The error might look like this:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\phptests\test.php:1) in C:\xampp\htdocs\phptests\test.php on line 4
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\phptests\test.php:1) in C:\xampp\htdocs\phptests\test.php on line 4
The most common reason for having problems with php sessions is that the php script is outputting something before the session is started.
Ok, you might not have any echo statements, or you might not even have ANY php code apart from reading the session ... but take time to check what happens BEFORE your code even begins.
For example, let's play spot the difference: (only one code will work)
<?php
session_start();
$_SESSION["test"] = "hello";
if($_SESSION["test"] == "hello";
{
echo "Session is working!";
}
else
{
echo "Session is NOT working!";
}
?>
and...
<?php
session_start();
$_SESSION["test"] = "hello";
if($_SESSION["test"] == "hello";
{
echo "Session is working!";
}
else
{
echo "Session is NOT working!";
}
?>
The difference is that there is a single space before the opening ANY data before session_start() will stop the script from working.
I've been there - having an accidental space or blank line at the top of the document will create serious issues, but enabling the php error outputs should show you where you are going wrong if this is the case.