Page 1 of 1

Script gives 2 different answers

Posted: Tue Jan 06, 2009 1:25 pm
by wizjr101
:banghead:

When i put the script on a page by itself it functions properly. When i put the script in a functioning page or call it in an include() it fails.
It does not matter where I put it on the page; at the top, bottom, or middle, before the head in the head after the head, or even in the body, it still fails.

Code: Select all

 
<?php
    $var1="empty";
    if (isset($var1=$_COOKIE["ivyleague"])){
        if($var1=="45"){
        header("location:http://ivyleaguesports.com");
        }
         }else{
        header("location:http://www.kraft.com");
                }
?>
 
I have a page that you start at. It sets the cookie then takes you too a specific page. What i need that page to do is check that the cookie is there and then load the page. IF not then send them to the main page for the cookie. The first page sets the cookie correctly. This I can double check and its there.

what could be going wrong? any ideas?

Re: Script gives 2 different answers

Posted: Tue Jan 06, 2009 2:24 pm
by jaoudestudios
When you say it fails, what actually happens? do you get an error? It could be your headers as your headers may already been sent.

Re: Script gives 2 different answers

Posted: Tue Jan 06, 2009 2:42 pm
by wizjr101
The script should find the cookie read it and validate the info then go to the page listed in the header. This works fine when the script is by itself. As long as i call the exact file readcookie.php it will find the site cookie and go to the page. If you call it or place in on ANY page with html or other php it wont work. It wont find the cookie. This is why I cant figure out what is going on. I've tried it out on many different pages. Even plain html - very basic <html><head></head><body></body></html>, causes it to not be able to find the cookie.

What I am looking to do is have the page look for and validate a cookie that was placed earlier. If its there then do nothing just load the page. IF its NOT there then send the person back to the login page.

I hope that make more sense.

Re: Script gives 2 different answers

Posted: Tue Jan 06, 2009 3:55 pm
by nvartolomei
Maybe error is in header()?

try
<?php
if (isset($_COOKIE["ivyleague"])) {
die("Cookie exists");
} else {
die("Where is my fuking cookie?")
}
?>
if this is working, the error is in header() function, because you can't send header to a browser after you sending body content.

Re: Script gives 2 different answers

Posted: Tue Jan 06, 2009 4:21 pm
by VladSun
Most probably you have already had some output (HTML, newline, whatever) before using the header() function.
You should use it before any output is transfered.

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 1:53 am
by jaoudestudios
Do you have errors turned on? Or check the error log!

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 8:24 am
by wizjr101
No errors... in the logs or shown. Errors is turned on.

Here is what I'm using and how i have it setup on a working page.

Code: Select all

 
 
<?php
    $var1="empty";
        if (isset($var1=$_COOKIE["il8"])){
            $var1=="45";
        }else{
            header("location:http://login.php"); 
        }   
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ivy League Compliance</title>
<link href="../ivy0808.css" rel="stylesheet" type="text/css" />
<link href="../menu.css" rel="stylesheet" type="text/css" />
</head>
 
 
<body>
Some text is here and the rest of the pages information.
</body>
</html>
 
 
If I put the script on a blank page and I run it as readcookie.php it finds the cookie and will does nothing. It will echo out the cookie info when i put 'echo' in the script so I know the script works.

Once the code is on this page it redirects, in essence; not see the cookie and sends the user to another page. Even though the cookie is there.

If I remove the script the page functions fine. I know I don't have anything wrong with the page. I know the script works by itself. How do I get the two to work together?

All I want to do is verify the cookie is there, if not send them back to the login page. Can this be done? Or can someone show me how to do it correctly.

I have even modified the script more to see if this might work. But this too has failed to find the cookie.

Code: Select all

 
<?php
        if (!isset($_COOKIE["il8"])){
            header("location:http://login.php");    
        }
?>
 
Just as above; this will always send me to the login.php even if the cookie is there.

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 9:39 am
by VladSun
Add exit() after header()

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 10:31 am
by wizjr101
didnt work.

I guess i just dont understand how cookies work or php just cant read cookies i dont know which.

Here is my final script after tinkering with it. It never reads or finds the cookie at all and just redirects to the kraft.com page. I can see the cookie and read it from the browser in FF or IE so I know its there, the script just cant find it. :banghead: :cry: If anyone can put me in the right direction as to how to do this I would be grateful.

Code: Select all

 
<?php
        if (!isset($_COOKIE["rchjr"])){
            header("location:http://www.kraft.com");
            exit();
        }else{
            exit(); 
        }   
?>
 
What the whole thing needs to do is this
1. User logs in and is verified.
2. cookie is then placed.
3. sent to home.php
4. home.php need to verify that an active cookie is there. if not send that person to the login page.
5. all other pages need to verify that an active cookie is there. if not send that person to the login page.

Does this work or do I need to do something completely different?
Thank you so far for everyone's help.

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 11:07 am
by nvartolomei
login file smth like this

Code: Select all

<?php
//setcookie
setcookie("logged", 'true');
echo "Hello!";
every page smth like this

Code: Select all

<?php
//on every page at the top
if (!isset($_COOKIE['logged']) || $_COOKIE['logged'] != 'true') {
    header("location: http://example.com/login.php");
}
//content
this is not the secure way!
i just showed how cookies works

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 11:37 am
by wizjr101
Ok I do understand then how the are suppose to work. ya! I just cant get them too. I've move everything over to a closed server with all error reporting on. This is a testing server and not open to the world so Im not worried about security at this point.

index.php - login page
loginprocess.php - verifies login then sets cookie. login failure goes back to index.php else it goes to readcookie.php
readcookie.php - this is the script that verifies there is a cookie. if not send back to index.php else exit script and display html.
Here is the loginprocess script. It works fine.

Code: Select all

 
<?php
 
        $username = $_POST['username'];
        $password = $_POST['password'];
        if($username=="james" && $password=="bond"){
        setcookie("rchjr","true",time()+3600);
        header("location:http://example.com/readcookie.php");
        }else{
        header("location:http://example.com/index.php");
        }
 
?>
 
No problems above here.

This is readcookie.php nothing else is on the page.
Its the reading of the cookie that wont function. per the last posted suggestion I changed it to this. It still does not work.

Code: Select all

 
<?php
        if (!isset($_COOKIE["rchjr"]) || $_COOKIE['rchjr']!='true'){
            header("location:http://www.kraft.com");        
        }
?>
 
Ive tried this on 2 different servers thinking it might be something there. no differences. I'm completely confused.

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 12:20 pm
by nvartolomei
readcookie.php redirects to another site, or you see blank page?

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 12:26 pm
by wizjr101
it redirects everytime.

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 12:39 pm
by nvartolomei
wtf?? all works!

Code: Select all

<?php
$username = $_GET['username'];
$password = $_GET['password'];
if (isset($_GET['logout'])) {
    setcookie("rchjr", "");
}
if ($username=="james" && $password=="bond") {
    setcookie("rchjr","true",time()+3600);
    header("location: readcookie.php");
} else {
    echo "To loggin go <a href=\"cookie.php?username=james&password=bond\">cookie.php?username=james&password=bond</a>";
}
?>
readcookie.php

Code: Select all

<?php
        if (!isset($_COOKIE["rchjr"]) || $_COOKIE['rchjr']!='true'){
            header("location:http://www.kraft.com");        
        } else {
            echo "Logged!<br>";
            echo "To sign out press <a href=\"cookie.php?logout\">here</a>";
        }
?>
example: http://lab.barttos.net/devn/cookie.php

Re: Script gives 2 different answers

Posted: Wed Jan 07, 2009 2:25 pm
by wizjr101
After everyone's help I have finally figured it out. I was putting the loginprocess.php file in a library folder - "functions", and calling it from there. Once i moved the file out and had it in the same folder as the other files it worked perfectly.

Apparently you cant go from login form and call the file from another folder and have it process the cookie you just put down. I just wont find it. here is how I had it setup originally.

root/login.php //form for the user name and password. form action="functions/loginprocess.php"
root/functions/loginprocess.php // validate login and set cookie then go to readcookie.php
root/readcookie.php // script that verifies the cookie is there, if not sends you back to root/login.php

I moved the loginprocess.php file to the root folder with the others and changed the form action="loginprocess.php". Once I did that everything worked. What this tells me is that you can't go outside the folder you set the cookie from. Which means it wont work in a hierarchy of folders.

I thought I had something wrong with my scripts. But once I compared what everyone else had written I knew I was right. Its just cant see outside its folder.

Thank you everyone for helping me out with this. Guess it's time that I go and play with sessions now, as this apparently wont do what I need it to do. Back to the drawing board I guess.

Again thank you to everyone.