Page 1 of 1

If cookies are not set...

Posted: Mon Jul 16, 2012 6:48 pm
by mixa2000
Can you please help me with this question, I am doing a small project and I need to "secure" my pages.
Can you give me an example of a script, how to allow those users that are logged in, and deny those who are not?
---
I already tried to use header for else, but it would say that Warning: header already set by...
Example of code:
<?php
if (isset($_COOKIE["username"]) AND (isset($_COOKIE["pass"])))
print "You are logged in!";
else
header("Location: login.php");
?>
---
Ask questions if you don't really get what I mean, I will try to explain it as we go. :) Thanks beforehand.

Re: If cookies are not set...

Posted: Mon Jul 16, 2012 7:32 pm
by requinix
mixa2000 wrote:Warning: header already set by...
You can only use header() and a few other functions if you haven't outputted anything yet. So move your login logic to a place before that happens.

Re: If cookies are not set...

Posted: Mon Jul 16, 2012 7:51 pm
by mixa2000
Okay I understood that part, but can you explain me briefly how you what do a php script so it will not allow for guests to visit some pages, and give an example code for that/those pages that would be allowed only for logged in users. Like the script that would check if they are logged in, then they can view the page, else they are not allowed and they get notified about that.

Re: If cookies are not set...

Posted: Tue Jul 17, 2012 5:43 am
by social_experiment
mixa2000 wrote:but can you explain me briefly how you what do a php script so it will not allow for guests to visit some pages, and give an example code for that/those pages that would be allowed only for logged in users. Like the script that would check if they are logged in, then they can view the page, else they are not allowed and they get notified about that.
Search the forum for "login scripts"; the topic you are looking for has been done quite a few times and most of those topics cover the basics pretty well

Re: If cookies are not set...

Posted: Wed Jul 18, 2012 12:32 am
by mixa2000
Thank you very much. :)