1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

PHP Session Help

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

  1. macrumors regular

    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


    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 6502a


    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. macrumors regular

    i tried it, the page shows up blank
  4. Moderator emeritus


    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

    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

    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. macrumors regular

    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


    Provide your code.
  9. macrumors regular

    I actually got it working so I'm ok for now thanks though! :)

Share This Page