2

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..?

2
  • Turn on error reporting and display. Put this at the very top of index.php after the opening <?php tag... ini_set('display_errors', 'On'); error_reporting(E_ALL);. FYI, you must call session_start() before any output (HTML, whitespace, etc) is sent to the browser. Commented Jul 1, 2011 at 6:38
  • your code doesnt have any bug. It is perfect. I suppose that your problem is with session_start(). As Phil said call session_start() at the very beggining of one.php before any output. a single space can also make it not to work. Commented Jul 1, 2011 at 6:48

5 Answers 5

2

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:-

  • Start the session in very new page, at the very beginning of the page, without any whitespace characters, using these lines of code:-

<?php

session_start();

// Other Lines of Code

  • Include another common page (common.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.

Sign up to request clarification or add additional context in comments.

Comments

1

$_SESSION is global. But it will only be initialized once session_start is called.

2 Comments

Ok.. if i add session start in index.php and if i set the session in one.php without addig session start in one.php.. will it work..?Since im just including the one.php in index.php where session already starts..
It will work in one.php, but not in index.php, or vice versa (not sure what you are doing). session_start doesn't just set the initial values of $_SESSION. It also sets a handler to save those values when the script ends.
0

session_start() has to be added right after starting first PHP file.

  • Make sure you do not have any whitespaces/characters before <?php
  • Turn on error reporting (1)
  • Try var_dump($_SESSION) (2)

(1):

<?php
error_reporting(E_ALL);

(2):

var_dump($_SESSION);

Comments

0

Add session_start as the first line in index.php. And it is not necessary to add in one.php since $_SESSION is global you can access the variable in one.php

Comments

0

For every Session session_start() at the top would be essential

session_start()

Try with

print_r($_SESSION)

You can debug the all SESSION value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.