0

I have a login page done in php and in that i am storing the username in cookie. But i cannot access this cookie variable in my home page which is also done in php, on thing i am doing all this in my localhost wamp server someone please help me to dothis

login.php

<html>
    <head>
        <link type="text/css" rel="stylesheet" href="login.css"/>
        <title> LOGIN Page</title>
    </head>
    <body>
    <div id ="fullscreen">
        <div id="container">
        <div id="heading">LOGIN</div>
        <div id="login-form" >
                <form  id="my-login"  action="" method="POST">
                <input type ="text" name ="user_name" placeholder="  Enter login id" class="text_box" id="usrname">
                <label id ="lusr_name" value="" name="lusr_name"></label><br>
                <input type ="password" name ="password" placeholder="  Password" class="text_box" id="paswd">
                <label id ="lpassword" value="" name="password" ></label><br>
                <input type ="submit" name ="login" value ="Log In" class="login_button" >
                </form>
        </div>
        <a href="signup.php" id="my_signup">Sign Up</a>
        </div>
    </div>
        <?php

        if(isset($_POST['login']))
        {
            $username = $_POST['user_name'];
            $pwd = $_POST['password'];

        if(empty($username) || empty($pwd))
            {
            if(empty($username))
            {
                echo "<script>document.getElementById(\"lusr_name\").innerText=\"Enter username\";</script>";
                echo "<script>document.getElementById(\"usrname\").className=\"text_errorbox\";</script>";
            }
            if(empty($pwd))
            {
                echo "<script>document.getElementById(\"lpassword\").innerText=\"Enter Password\";</script>";
                echo "<script>document.getElementById(\"paswd\").className=\"text_errorbox\";</script>";
            }
            }
        else
        {
        //setcookie( "TestCookie",$username,false,"/",false ); 
                setcookie( 'TestCookie', $username, time()+60*60*24*7, '/', 'localhost', false, true);

        include 'userdata.php';
        $obj = new userdata();
        $obj -> authent($username,$pwd);
        }
        }
    ?>

    </body>
</html>

homepage.php

<html>
    <head>
        <title>Login Successful</title>
        <>
    </head>
    <body style="background:#323B55">
        <h1 style="text-align:center ;color:#ffffff;font-family:arial;">LOGIN SUCCESSFUL!!!!!!!!!</h1>
        <?php
            if(isset($_COOKIE['TestCookie']))
            {
            echo 'Welcome ' . $_COOKIE['TestCookie'];
            }
        ?>
    </body>
</html>
2
  • You have to reload page to get cookie value Dec 24, 2013 at 8:54
  • actually i am redirecting to the homepage so its always reloding while login Dec 24, 2013 at 9:13

3 Answers 3

1

I think when setting your cookie, you should remove 'localhost' as cookie Domain. Just leave it empty. ''

EDIT:

Uhm, wait... setCookie() must be executed before ANY OTHER output has started! In your case you are outputting a lot of HTML before you do setCookie! Please execute the setCookie part first, or read up on Output Buffering.

Using setCookie after having started output should produce a notice/warning. But it could be that it has been suppressed.

3
  • but how do i save username before the html tags? Dec 24, 2013 at 9:23
  • @DmitriMendeleev If you really don't know how to re-arrange your code to execute setCookie before you do any output, then just put <?php ob_start(); ?> at the top of your file , and <?php ob_end_flush(); ?> at the bottom of your file. (Assuming you don't use die(); or exit(); in your code that would prevent you from reaching the bottom of your file.)
    – nl-x
    Dec 24, 2013 at 9:28
  • still it remains same !! Dec 24, 2013 at 9:35
0

Try using ths:-

This is an working example

$value="bikash";
setcookie("TestCookie", $value, time()+3600);  
echo $_COOKIE['TestCookie'];
0
0
<?php ob_start(); ?> <html>
<head>
    <title>Login Successful</title>
    <>
</head>
<body style="background:#323B55">
    <h1 style="text-align:center ;color:#ffffff;font-family:arial;">LOGIN SUCCESSFUL!!!!!!!!!</h1>
    <?php
        if(isset($_COOKIE['TestCookie']))
        {
        echo 'Welcome ' . $_COOKIE['TestCookie'];
        }
    ?>
</body></html> <?php ob_end_flush(); ?>

try this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.