single page application session_start error notification - php

I have a single page application based on an index.php page where I start the session and display first contents. Then I load via Ajax contents in different divs based on the links that are clicked.
If I want to access the $_SESSION array in the loaded page (eg. to display user's name) I need to put session_start also in the page that I load via Ajax.
Everything works fine this way but when I see the log I see that php is throwing an error each time I load one of the pages via Ajax saying that
"session already started. Ignoring session_start()".
So: on one side I need to put session_start to access the session array but on the other the session_start command is ignored.
From index.php:
<?php
require '../session_handler.inc.php';
require 'global_functions.php';
session_start();
if(isset($_SESSION['id1'])){
$actusr=$_SESSION['id1'];
}
A sample ajax call:
function loadContent(sourceUrl){
$(".container").load(sourceUrl);
}
The php I need on top of the called page:
<?php
require '../../session_handler.inc.php';
require '../global_functions.php';
session_start();
If I remove the session_start() the $_SESSION will be unavailable
How can I fix this situation? Can I access the session array from the loaded page without starting session?

It's better to use this line instead of session_start().
you might have more than one session_start and using session between those two.
if(session_id() == '') {
session_start();
}

If you are testing on a local WAMP or Xamp environment, I would recommend you clear your cache, close your browser (Not a single tab) and run your app.
Try this 1:
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
Or
2: If you have php>=5.4.0, better use this:
$status = session_status();
if($status == PHP_SESSION_NONE){
//There is no active session
session_start();
}else
if($status == PHP_SESSION_ACTIVE){
//Destroy current and start new one
session_destroy();
session_start();
}
Hope this helps..

move session_start(); to top of your page
<?php
session_start();
require '../session_handler.inc.php';
require 'global_functions.php';
if(isset($_SESSION['id1'])){
$actusr=$_SESSION['id1'];
}
session_handler.inc.php or global_functions.php may already contain session_start() so move the session_start() of index.php to the top. also note that this should be the first statement in index.php
or insert the code below at page top of index.php
<?php
session_start();
?>
and remove the other session_start() in index.php

Related

Understanding PHP files and AJAX calls

I'm slowly learning PHP ;-) I'm having difficulties understanding how separate PHP-files work together.
I make AJAX calls to different php files that all need to be connected to the backend (Parse). Such as:
sign_up.php
login.php
verify_email.php
get_something_out_of_the_database.php
What is the standard way to stay logged in over the different php files? (or what is the google search term for it..?)
Update:
Thanks for all your answers about 'sessions'. I doesn't work very well yet, so i made a new question.
Thanks!
Remzo
You should use PHP sessions. These are a way to store information on visitor browser between multiple pages...
To start a session, you first need to add session_start(); in every PHP file you intend to use it. Usually it's added in a header.php
Then, you can use sessions already.
To store a result:
$_SESSION['some_data'] = $var;
To retrieve a result in another page, for example:
echo $_SESSION['some_data']; // will echo $var
More info can be found here:
http://www.w3schools.com/php/php_sessions.asp
You can do this for example by storing the login-data in a session-variable and checking it at the start of every new page.
Example:
You check if login-data is valid. Then
session_start();
$_SESSION["login"] = $loginname;
At the start of another page:
session_start();
if(!isset($_SESSION["login"]) || $_SESSION["login"] != "check_somehow")
{
header("Location: logout.php");
exit;
}
For logging out you can use
session_start();
session_destroy();
On the start of your user logged in, you can do something like
session_start();
$_SESSION['USER'] = <some user info>;
In your other pages you can see if
if(isset($_SESSION['USER'])){
// do something
}
at last on logout
session_destroy();
will kill the session

Strange issue with session getting set in one place but not in another

I have two php pages for updating account, a frontend and a backend.
Front end (important part):
<?php
session_cache_limiter('none');
session_start(); //session gets started
include_once 'includes/db_connection.php';
include_once 'includes/signin.php'; //file that deals with login and creates the session variables
include_once 'includes/updateaccount_process.php'; //back end file
?>
Back end (important part):
<?php
include_once 'db_connection.php';
include_once 'signin.php';
?>
If I add session_start() to the back-end file I get a notice saying session already started. If I don't add session_start() the rest of the php script doesn't execute properly due to the dependency on the session variable.
If I add if(!isset($_SESSION)) { session_start(); }, it works perfectly, and I don't get any notice but I don't understand why.
Hope someone can help.
Thanks.
While PHP can generate front-end content, PHP resides on the back-end, i.e. you have are two server-side files. If your first PHP file starts a session, as long as your code doesn't destroy or otherwise disable that session, the session should exist as long as your browser stays open. When you go to another PHP page, if you run this code:
(!isset($_SESSION)) { session_start()
it will check to see if it makes sense to start a new session. If the session no longer exists, then a new one gets created. Running session_start() without checking for a previously set session will cause this error message to appear if the session still is in effect:
Notice: A session had already been started
This additional session_start() then should resume the current session.
There might be other reasons for session problems occurring. If you're using PHP5.4 or greater, you can call session_status() and its return value can indicate whether a session has been disabled or if none exists. It can also confirm whether one is currently active (see Manual).
Incidentally, the core contributor who devised session_status() was primarily concerned about providing users a way to check whether currently an active session exists. (see bug report.)
On the page you designate as "back end", I suggest redoing the code as follows:
<?php
if (!isset($_SESSION)) || (session_status() !== PHP_SESSION_ACTIVE) ) {
session_start();
}
include_once('signin.php');
include_once('db_connection.php');
You might consider moving include_once("signin.php") and placing it in the if-conditional block, as a statement following session_start(), as long as the included file only creates session variables and previous code doesn't unset them.
One final point, you may wish to use include() instead of include_once() if both of your pages for a fact only include each file once. Include_once() is slower than include(). You should use include_once only if your script has code that would result in an attempt to include the file more than once in a script (see here).

PHP $_SESSION variables single page

I'm trying to update session variables via some anchor tags to keep track of information to display. I currently am using $_GET variables, but would like to keep a clean address bar as this is a single page app. What I can't figure out is how to update the session variable and have it refresh the page and update the variable.
This is the only way I can think to do it, but it's not working as I expected.
Use a click.php file that grabs the $_GET variable and applies it to a session variable and redirects back to the index.php file.
index.php
<a href="click.php?msv=PhotoCount" >Photo Count</a>
click.php
if ( !empty($_GET['msv']) ) {
$msv = $_GET['msv'];
$_SESSION['meta'] = $msv;
header('Location: /');
}
Any thoughts?
You should start the php session explicitly at the beginning of both index.php and click.php, like this .. only then your session data will persist
<?php session_start(); ?>
If you choose to do it, consider removing session data later using this snippet
<?php session_destroy(); ?>
Add this to the top of index.php:
<?php
if(isset($_GET['msv']) && !empty($_GET['msv'])){
session_start();
$_SESSION['msv'] = $_GET['msv'];
}
?>
and give the target location of the link to the same page. So, there is no need of the second page.
If you are to use both the pages, give the target location of the link to the second page and add the this line at the end of the if statement.
header('Location: /index.php');

Php session problem

I m working on site which does not allow me to initialize session i-e whenever i write session_start(); the page does not load ?????
could you perhaps post a part of your code?
Also, session_start() has to be called before you send anything back to the user. Which normally means it should be on first line of your code.
<?php session_start(); ?>
Let's see what does the $_SESSION array store after we uncomment session_start() line:
<?php
error_reporting(-1); // Will report everything, comment out when not needed
ini_set('session.use_trans_sid', false);
session_start();
var_dump($_SESSION);
Maybe you need to put session_start after __autoload so that objects in $_SESSION are instantiated correctly (ie. not as stdclass.)

Restrict access to page with PHP

This seems really simple, and I see a lot of documentation about it, but I can't get it to work. Basically, I have a page "download-software.php" that we want only to be accessed from "download-registration.php" On the second page "download-registration.php" I have this:
<?php
session_start();
$_SESSION['authenticated'] = 'yes';
?>
and on the first page "download-software.php" I have this:
<?php
session_start();
if($_SESSION['authenticated'] != 'yes') {
header("Location: http://kinetick.com/V3/download-free.php");
};
?>
I need to kick the browser to the "download-free.php" page if they dont come from the first page. Can anyone help me out pls?
**Edit**
added session_start(); still doesn't work.
You need to add another session_start() to the beginning of download-software.php to resume the session you started from download-registration.php.
You forgot session_start() on download-software.php
You must always call session_start() before any html data to be able to use $_SESSION in your script

Categories