preet
preet

Reputation: 15

declaring session variables php

Is it not mandatory to use session_start() before using any session variables in PHP? I tried the following piece of code without declaring session_start() at the beginning, it worked fine.

So, now I'm confused. Please help !!!!

Also, I did not use any $_POST or $_GET to pass $uname to home.php, but still how does it work? If we use include 'home.php' then does it treat login.php and home.php as same page?

// code login.php//
<?
require_once 'db_connect.php';

if (isset($_SESSION ['user_id']) && !empty($_SESSION ['user_id']))
{

    $u_name = $_SESSION['user_name'];
    include 'home.php';
}
else
{
//some stmt
}
?>

/*******home.php file ****/

<?php
require_once 'dbconnect.php';
$_SESSION['username'] = $u_name;
//echo $_SESSION['username'];
//blah blah
?>

Upvotes: 1

Views: 4664

Answers (3)

Rogerio de Moraes
Rogerio de Moraes

Reputation: 1577

Use the session_start () is obligatory for every session in php. Being passed through a variable values ​​is not necessary to make POST or GET the same, since there is already the case the value increment. If not ouver value in the same session is null or blank, if you open the page in the same way the condition is wrong.

(!Isset($_SESSION ['user_id']) &&!​Is_Null($_SESSION['user_id']))

isset to check if empty this need! before, IF(!isset($_SESSION['user_id']) and in the second case would be to check if it is not null and void, for a session either exists or does not exist and if a value is set inesistente is null. So correct view is this: is_null($_SESSION ['user_id'])

Importantly, in the login page does not include but redirect to the page. in the case with a header.

Or could do everything in a single page, but it would not be legal to display on a page called login page. The default would be the index, ie if the login stay within a folder, you place it inside the index page and the address of the folder.

The reason for the session can still open is that sometimes the webserver does not realize that erased part of the code and loads of it from the system cache.

Upvotes: 0

Jonathan
Jonathan

Reputation: 3034

You definitely need it, if session.autostart is not set in php.ini. But you would probably know that then.

Do you not call it in db_connect.php? Also, I'm pretty sure you wouldn't get any errors, the session would just be empty.

Upvotes: 2

32bitfloat
32bitfloat

Reputation: 771

If you include a file via php, Session keeps active (as any others variables set too). If you would access this file as new request, you would need to set session_start(). This behaviour is because include and require act like moving the code of the included file into the current one, as you would have typed the code into one single file.

Plus: you don't need to require dbconnect.php twice.

edit: you asked about both files used as the same page - the page is the output given after the whole php code is done. The page itself doesn't care about how many files internally are used for generating it.

Upvotes: 1

Related Questions