user2881430
user2881430

Reputation: 367

redirect if session doen't exist

i m trying to redirect to attempt page if user fills incorrect information... on the other hand if attempt page got refreshed want it to be redirected on login page... i m using session for that...

if (isset($c))
{
    $q = mysql_query("select * from registeration where email='$a' and password='$b'");
    $r = mysql_num_rows($q);

    if($r)
    {
        $_SESSION["Authenticated"] = 1;
        $_SESSION['id'] = $a;
    }
    else
    {
        $_SESSION["Authenticated"] = 0;
        $_SESSION["att"] = 1;
    }

    if(isset($_SESSION["att"]))
    {
        header("location:attempt1.php");
    }
    else
    {
        session_write_close();
        header('location:profile.php');
    }
}

the above mentioned code is redirecting on attempt1.php but code on attempt1.php redirecting back to index.php

session_start();
if (!isset($_SESSION["att"]))
{
    echo "<meta http-equiv=\"refresh\" content=\"0; url=index.php\">";
}

i want attempt1.php code to redirect on user on index.php if session is not set.. and destroy session on refresh or direct page load... so that direct access to this page results in redirection to index page please help friends..

ANSWER ANSWER ANSWER all the questions i asked i made silly mistakes... here in this question i had not started session wile storing session variables.... add the below code in first line of login script

session_start();

Upvotes: 3

Views: 29795

Answers (3)

Anand Solanki
Anand Solanki

Reputation: 3425

Try this:

if(empty($_SESSION))
{
  header("Location: /mypage.php");
}
else
{
    // do something ....
}

- Thanks

Upvotes: 2

Prafulla
Prafulla

Reputation: 590

//try this code  
<?php 
session_start();
  if (!isset($_SESSION["att"]))
   {
      header("location: index.php");
   }
?>

Upvotes: 8

Matthew Riches
Matthew Riches

Reputation: 2286

I think your asking to redirect if a session doesn't exist, since a session requires an ID you should be able to check against that:

if(!(session_id()) header("Location: /mypage.php");

Upvotes: 4

Related Questions