Goddam cookies - cannot predict id before its too late

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
ElTorito
Forum Newbie
Posts: 5
Joined: Sat May 24, 2003 1:56 pm

Goddam cookies - cannot predict id before its too late

Post by ElTorito »

Ok - i have read the manual (allthough its not the most clear way).

1) i have a a form and a script for login (ID , passw)
2) i have a menu form and a script which is only allowed after the login form.

note: the login script could as well put out the menu form, so 1) and 2) would be the same.

In 2) the menu, i want to know/use the ID of the guy who logged in.

A cookie would be obvious (or a session).

The problem is the headers - after the login form its too late to set a cookie with the ID for the next form menu, because i have output HTML allready.

And no - i could put it in a GET request like: ?name=donald?id=duck - but then its visible in the adress bar.

how the heck is is possible to use a cookie for that ?? eg. setting a cookie with ID-data after some html jimbo.

it seems that the cookie thing is not well suited for such a login->menu->submenu->etc..

note:

A question might be: how often and when are the header sent. yes - i know at the first
instance of output. but wat if u jump to another html or php with the header(location....) ?

btw: pls don't send suggestions that ure not absolutely sure works.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

So what your saying is, you cant set cookies inside the page because you get a header error, saying they have already been sent?

If thats the case, then do what i and alot of other people do and use ob_start() ob_end_flush().

At the very start of the php file put this in:

Code: Select all

<?php
ob_start();
?>
At the very end of the file put this:

Code: Select all

<?php
ob_end_flush();
?>
All this does, is allows you to send headers from anywhere on the page.
ElTorito
Forum Newbie
Posts: 5
Joined: Sat May 24, 2003 1:56 pm

Post by ElTorito »

Sure - but that does'nt work

the testcook.php script accpts only 'ok' as valid login, set a accept cookie
and jumps to getcook.php that test for it as a accepted login, before
any further business...

(remember bout wat i said - only answers that work - pls)


SCRIPT: TESTCOOK.PHP - INPUT FORM FOR LOGIN

Code: Select all

<?php
ob_start;
if (isset($ID_name) and ($ID_name == 'ok')) { // ok - dude - accepted
      setcookie ('LoginID', $ID_name);
      header("Location: getcook.php");      
   }
else {                                                 // get id input from form
   echo '<HTML>';
   echo '<BODY>';
   echo '<p align="center">&nbsp;</p>';
   echo '<form method="POST" action="testcook.php">
        <div align="center"><div align="center"><center><p>Identifikation<br>
        <input type="text" name="ID_name" size="10"><br>
        Kodeord<br>
        <input type="password" name="ID_passw" size="20"></p>
        </center></div><div align="center"><center><p><input type="submit" value="Send" name="B1"></p>
        </center></div></div>
        </form>';
   echo '</BODY>';
   echo '</HTML>';
   }
ob_end_flush;
?>
SCRIPT: GETCOOK.PHP - RUN ONLY IF THE COOKIE IS SET/VALID

Code: Select all

<?php
if ($LoginID)  // no cookie =  no business
   {
    echo 'Cookie ok ! = ' . $_COOKIE[$LoginID];
   }
else
{
    echo 'no Cookie !';
}
?>
mod_edit: added php tags
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

sorry to say it buddy, but your question wasnt exactly crystal clear.

Now, where you have:

Code: Select all

<?php
if ($LoginID) // no cookie = no business 
{ 
echo 'Cookie ok ! = ' . $_COOKIE[$LoginID]; 
} 
else 
{ 
echo 'no Cookie !'; 
} 
?>
What is $LoginID ? That's not a valid variable, change it to:

Code: Select all

<?php

if ( $_COOKIE["LoginID"] )
{
   echo "Cookie ok ! = " . $_COOKIE["LoginID"];
}
else
{
   echo "No Cookie !";
}
?>
As far as my php knowledge goes you can't have a variable inside a global variable such as $_COOKIE.
ElTorito
Forum Newbie
Posts: 5
Joined: Sat May 24, 2003 1:56 pm

Post by ElTorito »

i knew that - but that doesn't change the prev. line which check for the cookie. - have u tried if the flush_ works - i did - and it does'nt work.
but i found i way to jump from one script/form to another scrip/form.
Post Reply