Jump to content

[SOLVED] Having trouble with "members only" sections with PHP script


dgs

Recommended Posts

Alright, so I installed this script that allows users to register to my site and gain access to their own profiles and member's only pages on my site.

The problem is:

When I put the code that makes the page members only on my page, if you aren't logged in, it asks you to. But even after someone logs in, it still asks them to log in.

 

But that doesn't happen in the directory of the log in page.

 

Alright, so, the log in page is in the directory "/members" of my site. So is the user's profile page.

When you go to the profile page not logged in, it asks you to log in. If you do, then the profile page shows your profile.

But any page not in the "/members" directory doesn't work with the code.

 

Here's the code to make a page member's only.

 

<?php
$username = $_COOKIE['loggedin'];
if (!isset($_COOKIE['loggedin'])) die("You are not logged in, <a href='/members/login.html'>click here</a> to login.");
echo "You are logged in as <strong>$username</strong>";
?>

I found this line in my login.php page that looks similar to that:

 

setcookie("loggedin", "".$_POST['username']."", time()+(3600 * 24));

 

Would I change it to something like this?:

 

setcookie("loggedin", "".$_POST['username']."", time()+(3600 * 24, "/"));

 

I suggest you, to read about Sessions.

Note that you can only set cookies, until headers are sent, i.e. until any output is generated(e.g html/whitespace before <?php, echos, etc)

Also, if a cookie is set using set_cookie, $_COOKIE will remain unchanged until the next request.

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.