Ryan Fung
Ryan Fung

Reputation: 2217

Get same PHP session ID for both html and php files

I have two files: html php

where html file is calling the php file to do something.

Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.

I would like to know how would I do so so that both html file and php file have the same PHP session ID.

Thanks for your help in advance.

Upvotes: 0

Views: 649

Answers (2)

TigerTiger
TigerTiger

Reputation: 10806

You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.

As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.

To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.

EDIT

To answer your qs in comments below - Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.

Myfile.php

<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
<a href="#">Go to next page (<?php echo $_GET['next_page']; ?>)</a>
......
</body>
</html

so you just open and close php tags where you needed php stuff to go.

To print session ID - just use somewhere in the HTML of PHP file.

Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.

Upvotes: 2

Sumesh TG
Sumesh TG

Reputation: 450

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 will run great.

Upvotes: 2

Related Questions