Piklu Guha
Piklu Guha

Reputation: 577

Using php session variable in different pages

I created a session variable in a php page 'session1.php' as follows

<?php
session_start();
$_SESSION['name']="piklu";
?>

now I want to access the session value in another page say 'session2.php'.i need suggestion for that. what I have done in 'session2.php' is as follows.

<?php
include('session1.php');
echo $_SESSION['name'];
?>

The above code is working but only when session1.php and session2.php doesn't contain any html tags.if it is then all html tags from 'session1.php' will be copied to 'session2.php'. Is there any other way to do that?

Upvotes: 0

Views: 109

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324810

You don't need to include all of session1.php. instead, just call session_start() at the start of session2.php.

Upvotes: 6

Related Questions