PHP sessions with HTML

前端 未结 4 1899
温柔的废话
温柔的废话 2020-12-31 22:44

I have a website which uses PHP and HTML pages, I want to create a session which stores a username from the login page. But the login pages are php and the next pages are ht

相关标签:
4条回答
  • 2020-12-31 22:54

    If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:

    # Add this to public_html/.htaccess file
    AddHandler application/x-httpd-php .html
    AddHandler application/x-httpd-php .htm
    

    You should be able to reload the html page and your PHP code (from Michael Matthews answer) will run great.

    0 讨论(0)
  • 2020-12-31 22:54

    You are trying to share a PHP session variable with a page that is of type text/html. As you suggested you must make the HTML page a PHP page for this to work and add a little snippet of PHP somewhere to display the user name.

    Change your HTML page to PHP. At the top of the page add something like this:

    <?php
      session_start(); // must be before any output
      $username = $_SESSION['username']; // or whatever you called it
      // check that $username is valid here (safe to display)
    ?>
    html here
    Hello <?= $username ?>!
    
    0 讨论(0)
  • 2020-12-31 22:56

    You can't put php into .html files without playing around with your server's configuration files. You should only put php into .php files.

    If you have a lot of .html files, you can simply rename them to .php files. It's okay to put pure html into something.php. So, you should make sure that all of your files end with .php, and then you can put any session logic you want into them.

    0 讨论(0)
  • 2020-12-31 23:17

    As the sessions are handled by PHP, it needs PHP to maintain the state. You need at least session_start() to use the session variables stored in $_SESSION.

    0 讨论(0)
提交回复
热议问题