0

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.

9
  • 3
    It's usually helpful when you say "my (blank) is not working" if you explain what it is or isn't doing; If it's giving an error, tell us the error! Commented Jul 10, 2015 at 6:00
  • @DaymonSchroeder Below is answer of my question. I sort out my problem from that answer Commented Jul 10, 2015 at 6:05
  • In IF you have to use Comparison operator not assignment operator change $_SESSION['UserID']='2' to $_SESSION['UserID']=='2' Commented Jul 10, 2015 at 6:05
  • Your problem occurred because of a typo. Commented Jul 10, 2015 at 6:06
  • 1
    @KanishkaPanamaldeniya Stackoverflow said to wait for couple of minute after i will mark as accept your answer. Thank you so much Commented Jul 10, 2015 at 6:08

1 Answer 1

2

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;
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.