SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    canada
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    initiating a session variable inside a class

    Hello,
    I have a basic class which verifies the login of a user and for most part it works.

    The class I'm working on, basically cleans up the input fields and checks the user id/password against the database and so far this works.
    after checking with the database I like to set the user name to a session variable. Here's where its not working, the script itself is very basic nothing complicated either case I copy/pasted the part im having issues with it below.

    if the user is confirmed the page is rerouted to another page which has a the following line of code to confirm the session variable
    echo $_SESSION['test1']; //testing

    PHP Code:
    public function    userLogin()
        {      try 
                {     
    //database select statement which i took out for security reason but it was tested several time to make sure it works and it works.
                    
                    
    if(empty($DBresults)) 
                    {    
    $this->errorStatus[] = 'user Not found'// for testing
                        
    [B]$_SESSION['test1'] = "Not found";    // for testing[/B]
                                    

                    else 
                    {    
    $this->errorStatus[] = 'found'// for testing
                    
    [B]    $_SESSION['test1'] = "found"// for testing[/B]
                    
    }        
                    
                }
                catch(
    PDOException $e) {  echo $e->getMessage();  }            
        } 
    the highlighted code should set the session variable based on the resluts but its not..

    Can you not set a session variable within a class-> method, is it out of scope???

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,903
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Have you used session_start() before anyplace where you're using the $_SESSION array?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2013
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Answering your question "Can you not set a session variable within a class-> method, is it out of scope???".

    You can set / get the value anywhere in your application because $_SESSION is a super global array the same as $_POST an $_GET. For more information see here.

    Also what is the [B] in your code?? Is that when you copy and pasted it from your text editor??


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •