Jump to content

[SOLVED] Cookies Drivimg Me Crazzzzzy!


steviez

Recommended Posts

Hi,

 

I need my users to be logged in to access some pages on my site, So i have made this simple code to do it:

 

<?php
function loggedin() {
    if(!isset($_COOKIE['loggedin']))
    {
    header("location: $secure_url/login.php");
    }
?>

 

But it will not work, I am using PHP5 so im not sure if thats a problem. Please can someone help! :)

Link to comment
https://forums.phpfreaks.com/topic/101191-solved-cookies-drivimg-me-crazzzzzy/
Share on other sites

first off it won't work cause your function does not know $secure_url. You would have to pass the variable to the function or make it global

 

pass it

function loggedin($secure_url) {
    if(!isset($_COOKIE['loggedin']))
    {
    header("location: $secure_url/login.php");
    }
}

loggedin("https://mysiite.com");

 

Or use global

function loggedin() {
global $secure_url;
    if(!isset($_COOKIE['loggedin']))
    {
    header("location: $secure_url/login.php");
    }
}

 

Ray

 

 

instead of isset maybe you should check the value.

if(!isset($_COOKIE['loggedin']) || $_COOKIE['loggedin'] IS NULL || $_COOKIE['loggedin'] == " ")

 

have you tried to echo the cookie and see what it is set at so you can compare it??

 

Ray

something even more strange is that if i do this:

 

<?php
function loggedin() {
    if(!isset($_COOKIE['loggedin']))
    {
    print("You are not logged in");
    #header("location: $secure_url/login.php");
    }
?>

 

It works and prints it out for all that are not logged in.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.