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>";
?>

Link to comment
Share on other sites

There's a few parts to the script.

Which part do you want?

 

For the log in, there's the HTML form that submits the log in information, and there's the PHP page that the information gets sent to.

Link to comment
Share on other sites

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, "/"));

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.