I have one index.php , one.php file.
In one.php i have starting the session and setting the session var:
session_start();
if(isset($_GET['user'])){
$_SESSION['user'] = $_GET['user'];
}
function getUsername(){
return $_SESSION['user'];
}
Im including one.php in index.php after im calling the function getUsername()
im not starting session in index.php
include_once('one.php');
echo getUsername();
But im not getting the session in index.php. Why ? im passing the the variable as
index.php?user=newuser .
is it not possible to get the session in all pages with out starting session . is it any possible method to get a global variable from any where, if i set it once..?
No it is not possible to get the session in all pages without starting session. So basically, you need to follow either of the following two ways:-
<?phpsession_start();// Other Lines of Codecommon.php) which has the line (session_start();at the very beginning of that common page) at the beginning in every new page.So basically, this particular statement (
session_start();) is the heart of using the session.Now according to your question, the page “
one.php” must be included at the very beginning. So it should be working.Hope it helps.