PHP Session Help

Discussion in 'Web Design and Development' started by GhostDZ9, Nov 4, 2010.

  1. macrumors regular

    Joined:
    Sep 13, 2010
    #1
    Hey guys,

    I am currently design a Web Based CMS, and I need to figure something out.

    I have one file for login form in html, it does the validation of the login in to a php file then it loads another page if it is entered in correctly.

    This is the code for the redirection part

    "header("location:main.php");

    Now what i want to do is pass the user name from the login form to the main.php page so i can display welcome (username)!

    can someone help me?
     
  2. macrumors 65816

    aarond12

    Joined:
    May 20, 2002
    Location:
    Dallas, TX USA
    #2
    You can easily pass values from page to page using hidden input values, populated by your scripts.

    Page 1:
    <input type="text" name="yourname"/>
    [submit the form]

    Page 2:
    <input type="hidden" name="yourname" value="<?php echo @$_POST["yourname"] ?>"/>

    You can always use cookies (which is likely the better choice in this case).
     
  3. thread starter macrumors regular

    Joined:
    Sep 13, 2010
  4. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    If you're using sessions, then you should be able to just access $_POST['username'] or whatever you named the field for the user name. Just stick this in the session, then you can retrieve it later on the redirected to page. This will work as long as you are using sessions correctly.
     
  5. macrumors newbie

    Joined:
    May 15, 2010
    #5
    When you redirect a page using header, the POST won't be recognised. It is only valid on the page which redirects to main.php. So what you can do is retrieve the information on main.php from the database, or set $_SESSION['username'] = $_POST['yourname']; on the login page then on main.php requesting $_SESSION['username'] for the welcome username.
    What you shouldn't forget is session_start() at the beginning of each page, so PHP knows when to use the sessions.
     
  6. macrumors 6502

    Joined:
    Jun 23, 2003
    #6
    Using inputs is a bad idea from a security perspective, since a user can change that to whatever they want and resubmit the page.

    As the above poster has noted, using session variables in PHP is dead-easy. Just call session_start() somewhere near the start of your script, then you can access the _SESSION array to store or retrieve whatever you want.
     
  7. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #7
    Yeah thats what i thought to I have it starting at the beginning of my script but it doesn't seem to access the information. may be i have it starting in the wrong page.

    I have the session starting in the main.php page

    any ideas?
     
  8. Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #8
    Provide your code.
     
  9. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #9
    I actually got it working so I'm ok for now thanks though! :)
     

Share This Page