Do PHP variables ever get deleted?

Discussion in 'PHP' started by Imozeb, Feb 16, 2010.

  1. #1
    Do PHP variables ever get deleted when the user navigates to a new page. For example if I use PHP redirect command will that reset all the $variables or is there a way I can get them?
    And can I delete them?
     
    Imozeb, Feb 16, 2010 IP
  2. ryantetek

    ryantetek Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use Sessions if you want a certain variable to be passed on one page to another.
    use the unset() function to delete variable and sessions.
     
    Last edited: Feb 16, 2010
    ryantetek, Feb 16, 2010 IP
  3. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What Ryantetek means is:

    Variables will never be 'deleted' from the actual code unless you physically delete them yourself; the unset(); function will 'temporarily delete' a variable, assuming that it exists

    As far as passing variables from one page to the other, you use sessions:

    sesssion_start(); // At the very start of the files that you are using sessions on
    PHP:
    then you just:

    Page one

    <?php
    session_start();
    $_SESSION['username'] = 'Marshton';
    ?>
    
    PHP:
    Page two:

    
    <?php 
    session_start();
    echo $_SESSION['username'];  //If you went to page one before visiting this page, the output is 'Marshton'
    ?>
    PHP:
    Hope this helps!
     
    Marshton, Feb 16, 2010 IP
  4. Izonedig

    Izonedig Member

    Messages:
    150
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #4
    Yes, use sessions, or you can even use cookies to keep the variables after the user exit the page and return few days later :)
     
    Izonedig, Feb 16, 2010 IP
  5. davebrwn

    davebrwn Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes also there is one function in php that delete all session variables that is session_destroy()

    Thanks,
     
    davebrwn, Feb 16, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Okay. thanks everyone.

    ~imozeb :)
     
    Imozeb, Feb 16, 2010 IP
  7. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Cookies are better at storing data for longer periods of time; however, I would suggest NOT using Cookies because they can easily be manipulated.

    If you need to keep data for more than 5 days on a user, personally I'd store the information in a database.

    Just a thought.
     
    Marshton, Feb 17, 2010 IP