sunny
sunny

Reputation: 1593

How to use session in PHP with variables

I am new in PHP. I want to use session with variable. Its my first experience to play with session. I create 4 pages.

1. is session1.php

<?php
 session_start();
$_SESSION['UserID']='1';
?>

2. is session.php

<?php

 // starts session
 session_start();
 if($_SESSION['UserID']='1')
{
    header("location: user.php");
    }
    if($_SESSION['UserID']='2')
{
    header("location: mang.php");
    }
?>

3.user.php

<?php

echo "This is User page";
?>

4.mang.php

<?php
echo "This is manager page";
?>

In my code there is problem of IF condition. My IF condition is not working. Can you guys please help to sort out my problem.

Upvotes: 0

Views: 80

Answers (1)

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17586

change

if($_SESSION['UserID']='1') and if($_SESSION['UserID']='2')

to

if($_SESSION['UserID']=='1') and if($_SESSION['UserID']=='2')

And also you have started session in session1.php , so no need to start the session again in session.php

Final Output

<?php

 include ("session1.php");

 if($_SESSION['UserID']=='1')
 {
    header("location: user.php");
    exit;
 }
 if($_SESSION['UserID']=='2')
 {
    header("location: mang.php");
    exit;
 }

Upvotes: 2

Related Questions