If cookies are not set...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

If cookies are not set...

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: If cookies are not set...

Post 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.
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: If cookies are not set...

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: If cookies are not set...

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: If cookies are not set...

Post by mixa2000 »

Thank you very much. :)
Post Reply