Access PHP session variables in html

Discussion in 'HTML & Website Design' started by kiblawi, Jul 27, 2011.

  1. #1
    I am building a website where a user logs on from the index.html page. When the user logs on a php $_SESSION['username'] variable is created and then the site redirects the user to a members.php page. If the user returns to the index.html how do I check if the $_SESSION['username'] variable has already been created so the site will redirect the user back to the members.php page.
     
    kiblawi, Jul 27, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    You cannot access/use any PHP code in a HTML file.

    What you need to do is rename index.html to index.php, from there you can use PHP code.
    The server will still recognize index.php as the default page of your site.
     
    JohnnySchultz, Jul 28, 2011 IP
  3. Torafox

    Torafox Peon

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that's wrong. sure you can access PHP variables in HTML. you just need to configure your webserver to send HTML files through PHP as well. then you can for example do


    <?php if (isset($_SESSION["xyz"]))
    {
    // do something
    header("Location: /members.php");
    }
    else
    {
    // do something else - or not
    }
    ?>

    <html>

    ..some HTML...
    ..some more HTML

    </html>


    be aware that the 301 redirection has to happen before any HTML is echoed, otherwise you'll get a PHP error saying "headers already sent".
     
    Torafox, Jul 28, 2011 IP
  4. kiblawi

    kiblawi Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for all the good advice! just for future reference, is there any way to access php variables through javascript?
     
    kiblawi, Jul 28, 2011 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #5
    Ajax would do.
     
    ketting00, Jul 29, 2011 IP
  6. sudhisinha

    sudhisinha Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you can define this type
    if(isset($_SESSION['username']))
    {

    /* condition................
    }
     
    sudhisinha, Aug 27, 2011 IP