Access PHP session variables in html

Discussion in 'PHP' 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. suryawl

    suryawl Peon

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    i don't think you can do that. session is for php only. i really don't see any reason why you wanna use index.html. using index.php is just the same
     
    suryawl, Jul 27, 2011 IP
  3. Ovidiu20

    Ovidiu20 Active Member

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #3
    You cannot use PHP variables on a .html page. You need to save it like index.php then you can check if session is registered:
    if(session_is_registered("username")){header ("Location: members.php");}
    PHP:
     
    Ovidiu20, Jul 27, 2011 IP
  4. ZeroGamma

    ZeroGamma Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    PHP SESSION variables can only be used in PHP parsed pages. You can either rename your .html page to .php, or modify your Apache config/.htaccess file to interpret all .html files as PHP code. I personally would not recommend the later for security reasons.
     
    ZeroGamma, Jul 28, 2011 IP
  5. kiblawi

    kiblawi Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
  6. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #6
    Since php will process before any javascript as it is server-side, then you can simply echo php variables into javascript like this:

    
    alert('<?php echo $_SESSION['My_Session_Var']; ?>');
    
    PHP:
     
    Thorlax402, Jul 29, 2011 IP
  7. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Yes it could be done with ajax/javascript. Your making it more complicated then it really needs to be. Just rename index.html to index.php or use the .htaccess trick of making .html into .php type of files. Then you can just reference the session data yourself using php.

    Interesting google finds over this very subject. Excellent reading too.

    php to html (html that works like php ext)
    http://www.besthostratings.com/articles/php-in-html-files.html

    ajax check to see if session var has changed.
    http://stackoverflow.com/questions/3508587/php-ajax-check-if-session-variable-changed

    great place to start about php sessions
    http://www.php.net/manual/en/function.session-start.php

    You could also use php to set cookies and then use ajax to access those cookies var's, but that is way insure.
     
    exodus, Aug 8, 2011 IP
  8. ModulesGarden

    ModulesGarden Active Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    58
    #8
    I don't think this is useful to use such a modifications. Why don't you just change index.html to index.php ? It takes just few seconds.
     
    ModulesGarden, Aug 8, 2011 IP
  9. Andre91

    Andre91 Peon

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Hey OP, follow these steps:
    1) Rename your index.html file, to index.php
    2) Add the following PHP code to the very top of your now, index.php file:

    <?php if(session_is_registered("username")){header ("Location: members.php");} ?>

    This code must be at the very top of the page. Not even a space must appear before the code. Try it and GL.
     
    Andre91, Aug 8, 2011 IP
  10. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #10
    start_session has to be the first thing unless php.ini has it set to auto start then Andre91 code is correct.
     
    exodus, Aug 8, 2011 IP
  11. ausrixy

    ausrixy Peon

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #11
    just use
    if($_SESSION['username']){ }
    or if(strlen($_SESSION['username'])>3){ }
     
    ausrixy, Aug 10, 2011 IP