Jump to content

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

 

 

Have you tried from other machines. I have seen cookie stay set forever. Maybe in your code somewhere you should make sure your named cookie is unset before you set it again. can you post code on how you are setting the cookie

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.