Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 904
user2881430 Avatar asked Apr 25 '26 17:04

user2881430


2 Answers

//try this code  
<?php 
session_start();
  if (!isset($_SESSION["att"]))
   {
      header("location: index.php");
   }
?>
like image 152
Prafulla Avatar answered Apr 28 '26 06:04

Prafulla


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");
like image 37
Matthew Riches Avatar answered Apr 28 '26 05:04

Matthew Riches



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!