2

I set a SESSION inside a function and I'm trying to accses it from another function. I think that the session is set only for that function, can i do anything about it?

thx!

the code:

    $admin_password = sha1(md5('1234')); // סיסמת מנהל
    function login($password,$admin_password)   {
        $password = sha1(md5($password));
        if ($password == $admin_password)   {
            $_SESSION['admin_ver'] = $admin_password;
            return true;
        }   else    {
            return false;
        }
    }

    function log1($admin_password)  {
        if (isset($_SESSION['admin_ver']))  {
            if ($_SESSION['admin_ver'] == $admin_password)  {
                return true;
            }   else    {
                return false;
            }
        }   else    {
            return false;
        }
    }

EDIT: I tried to run this page on another server and it worked. Can anyone suggest the reason? thx again!

9
  • 4
    Make sure you have added session_start() on top of the page Commented Jan 18, 2014 at 7:48
  • 1
    Session is a global variable. It can be accessed anywhere and is not limited by scope Commented Jan 18, 2014 at 7:48
  • There is session_start() at the top. It still not working for some reason... Commented Jan 18, 2014 at 7:50
  • Are you sure that function login is called first ? Commented Jan 18, 2014 at 7:52
  • Pheeew it was really tough to copy this -> // סיסמת מנהל so anyone interested in knowing what does it mean, it means "// Password Manager" Commented Jan 18, 2014 at 7:52

3 Answers 3

1

I advise you read the basics of a session variables: http://www.w3schools.com/php/php_sessions.asp

A session variable is completely global to that page visit (aka a session), any page/function can access it which has called session_start();

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

4 Comments

I know the basics, but i couldn't think of anything else that can cause it.
"I think that the session is set only for that function" this is not the case, session variables don't work like that. It's more likely a get/set issue.
I didn't explain myself well. I tried to say that SOMEHOW it's set only for the function.. because i can't find any problem with the code.
Please read why w3schools is bad and link the official documentation. tia!
0

I suggest you put this code <?php session_start(); ?>on every page you want to use your $_SESSION variables.

I works for me all the time so it should work with yours also. And take note: you should use session_start(); function whenever you want to use $_SESSION variables. Good Luck!

Comments

0

After seeing your latest edit, I am thinking that php on your server is not configured properly to support sessions,

To configure, open php.ini file and define the following params,

Define path to save sessions

session.save_path = "C:\php\sessiondata\"

Define handler

session.save_handler = files

Whether to use cookies.

session.use_cookies = 1 

Name of the session (used as cookie name).

session.name = PHPSESSID

Define Lifetime in seconds of cookie or, if 0, until browser is restarted.

session.cookie_lifetime = 0

The path for which the cookie is valid.

session.cookie_path = /

The domain for which the cookie is valid.

session.cookie_domain =

Handler used to serialize data. php is the standard serializer of PHP.

session.serialize_handler = php

MORE ON SESSION CONFIGURATION : http://www.php.net/manual/en/session.configuration.php

REF: http://support.qualityunit.com/021373-How-To-Enable-Session-Support-for-PHP

SIMILAR HELPFUL THREAD: PHP.ini example to enable sessions?

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.