Quantcast

Forums | MacLife

You are not logged in.

#1 2009-08-16 1:12 pm

Pithecanthropus
Roast Master
From: St. Cloud, MN
Registered: 2002-12-30
Posts: 4449
Website

More Learning: PHP Help

I am in the very rudimentary steps of learning PHP/MySQL and have run into a problem with cookies. First of all I am running all my PHP through MAMP, I have the feeling that might wind up being an important point here, but I could be wrong.

I am setting up a very basic page with a simple test cookie named 45 that expires in a week. Here is my code*:

Code:

<html>
    <head>
        <title>Cookies</title>
    </head>
    <body>
    <?php 
        setcookie('test', 45, time()+(60*60*24*7)); 
    ?>
    </body>
</html>

And this is the error message I get from Firefox and Safari.

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/php_sandbox/cookies.php:6) in /Applications/MAMP/htdocs/php_sandbox/cookies.php on line 7

I can't figure out what's wrong. Can anyone help?


*some of you may recognize this from the Lynda.com training videos.


Grandfatherly advice:  You can drink 'em pretty, but you can't drink 'em smart.

Offline

 

#2 2009-08-16 2:25 pm

Booksley
Zombie Genocidest
From: Toronto, Ontario
Registered: 2001-02-16
Posts: 5037

Re: More Learning: PHP Help

Code:

<?php 
    setcookie('test', 45, time()+(60*60*24*7)); 
?>
<html>
    <head>
        <title>Cookies</title>
    </head>
    <body>
    
    </body>
</html>

You need to send any headers before sending data, and cookies are part of the header. That's why you got that error message.

Offline

 

#3 2009-08-16 3:27 pm

Pithecanthropus
Roast Master
From: St. Cloud, MN
Registered: 2002-12-30
Posts: 4449
Website

Re: More Learning: PHP Help

Ah ha!

Thanks.


Grandfatherly advice:  You can drink 'em pretty, but you can't drink 'em smart.

Offline

 

#4 2009-08-19 4:52 pm

Pithecanthropus
Roast Master
From: St. Cloud, MN
Registered: 2002-12-30
Posts: 4449
Website

Re: More Learning: PHP Help

I've run into the same problem again, but this time with a page redirect inside a form. We've filled out a small form and if we're successful we want it to redirect to a central page called "content.php".

Code:

<?php
    $menu_name = $_POST['menu_name'];
    $position = $_POST['position'];
    $visible = $_POST['visible'];
?>
<?php
    $query = "INSERT INTO subjects (
                menu_name, position, visible
            ) VALUES (
                '{$menu_name}', {$position}, {$visible}
            )";
    if (mysql_query($query, $connection)) {
        // Success!
        header("Location: content.php");
        exit;
    } else {
        // Display error message
        echo "<p>Subject creation failed.</p>";
        echo "<p>" . mysql_error() . "</p>";
    }
?>

So if I can't modify the header information, how am I supposed to do this? (Man, if I can't handle this beginner crap, how am I ever going to learn this stuff? mad )


Grandfatherly advice:  You can drink 'em pretty, but you can't drink 'em smart.

Offline

 

#5 2009-08-19 6:12 pm

sturner
Royal High Poobah
Moderator
From: Carrollton, TX USA
Registered: 2000-01-31
Posts: 13778

Re: More Learning: PHP Help

The following is a totally fatuous, and unhelpful reply

Magic


I'm not dead yet.
There are 3 types of people, those who can count and those who can't.
"There are few things graven in stone, excepting your date of death."

Offline

 

#6 2009-08-19 7:54 pm

Basseq
Fixxy of F&S fame
From: D.C.
Registered: 2002-12-18
Posts: 3125
Website

Re: More Learning: PHP Help

You're sending data to the page before calling header(). Find it.


Basseq is me, John Whittet.
(Finishing the remainder of the thought expressed in the post has been left as an exercise for the reader.)

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson