Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP sessions with HTML

Tags:

html

php

session

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 html.

Is this a problem or can I just add a small statement of php into the html page saying

 <?PHP session_start();
$_session['loginid']=$_post['username'];
?>

Or am I doing it wrong?

This is the first time i've used sessions and they confuse me a little.

thanks for any help.

like image 853
user45344 Avatar asked Jan 19 '09 18:01

user45344


People also ask

What are sessions in HTML?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

Why session_start () is used in PHP?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

What is PHP session_start () and Session_destroy () function?

Output: session_destroy() function: It destroys the whole session rather destroying the variables. When session_start() is called, PHP sets the session cookie in browser. We need to delete the cookies also to completely destroy the session. Example: This example is used to destroying the session.

What can I use instead of session in PHP?

The alternative to sessions is cookies (in fact, sessions are usually implemented using cookies).


3 Answers

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.

like image 200
localshred Avatar answered Sep 29 '22 23:09

localshred


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 ?>!
like image 32
Michael Mathews Avatar answered Sep 29 '22 23:09

Michael Mathews


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.

like image 30
Gumbo Avatar answered Sep 30 '22 00:09

Gumbo