tags:

views:

74

answers:

4

For some reason This php script won't echo my cookie variable:

<?php
    require 'connection.php';
    require 'variables.php';

    $name = $_POST['name'];
    $pass = $_POST['pass'];

    if(($name == $admin_name) && ($pass == $admin_pass)){
        setcookie($forum_url."name",$name,time()+604800);
        setcookie($forum_url."pass",$pass,time()+604800);
    }

    else
        echo 'Failed';
?>

heres the html that gets sent to admin_login.php

<form method=post action=admin_login.php>
            <div id="formdiv">
                <div class="fieldtext1">Name</div>
                <div class="fieldtext1">Pass</div> 
                <input type="text" name=name size=25 /> 
                <input type="password" name=pass size=25 />
            </div>
            <input type=submit value="Submit" id="submitbutton">
        </form>

here is the index, where I want the info echoed

<?php echo $_COOKIE[$forum_url."name"]; ?>

What am I doing wrong?

A: 

Make sure you set the path for the cookie.

If you set cookie in the one path, but you are trying to get it from different path, it won't work.

Can you let me know the URL for the index and where you set cookie?

Hery
Also, subdomains.
Matchu
they're getting set in the same domain and sub-domain
William
how about the path?
Hery
+1  A: 

Have you tried var_dump($_COOKIE) at the point where you're trying to spit out a specific cookie value? Is it possible that $forum_url hasn't been defined yet at the point where you're either setting the cookie, or trying to echo out its value? Perhaps the cookie's been set to name and pass because $forum_url is blank.

Marc B
A: 

Also check that the headers haven't already been sent when calling setcookie() by asserting that headers_sent() returns false. Setting a cookie occurs within the HTTP header, so make sure you do so before any output is generated.

For instance:

<?php require 'connection.php'; require 'variables.php'; ?>
<h1>Hello world!</h1>
<?php setcookie($forum_url."name",$name,time()+604800); ?>

Will not work, because output has already been passed through to the HTTP body by the time setcookie() is called.

Paul Lammertsma
A: 
Levi