hi when i am tring to run this script then could not setup cookies don't know why somebody plz help

<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>

<p><a href="aba.php">Register</a> | <a href="login.php">Login</a></p>
<h3>Login Form</h3>
<form action="" method="POST">
Email: <input type="text" name="email"><br />

<input type="submit" value="Login" name="submit"><br />
</form>
<?php
if(isset($_POST["submit"])){

include 'dbconnect.php'

if(!empty($_POST['email']) && !empty($_POST['pass'])) {
    $ename=$_POST['email'];
    $pass=$_POST['pass'];
    $securepass=md5($pass);



    $query=mysql_query("SELECT * FROM users WHERE user_email='".$ename."' AND user_pass='".$securepass."'");
    $numrows=mysql_num_rows($query);
    if($numrows!=0)
    {
    while($row=mysql_fetch_assoc($query))
    {
    $dbename=$row['user_email'];
    $dbpassword=$row['user_pass'];
    $dbusername=$row['user_name'];
    }

    if($ename == $dbename && $securepass == $dbpassword)
    {
        if (!isset($_COOKIE["testcookie"])){
        setcookie("testcookie",$dbename,$dbpassword,$dbusername,mktime()+86400,"/") or die("could not set cookie" );
        /* Redirect browser */
    header("Location: member.php");


        }
        else{echo "plz enable your cookies on the browser";}
    }


    }else {
    echo "Invalid username or password!";
    }

} else {
    echo "All fields are required!";
}
}
?>

</body>
</html>

Recommended Answers

All 5 Replies

From the manual:

"Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace."

sorry can u plz more specific plzzzzzzzzzzzzzzzzzzzzzzzz

What part don't you understand? setcookie must be called before any output, so before lines 1-14.

but session is working don't know whey cookies isn't work

session is working on same statement but don't know whey cookies is not working

<!doctype html>
<html>
<head>
<title>Login</title>

</head>
<body>

<p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>
<h3>Login Form</h3>
<form action="" method="POST">
Email: <input type="text" name="ename"><br />
Password: <input type="password" name="pass"><br />   
<input type="submit" value="Login" name="submit"><br />
</form>
</body>
</html>


<?php

include'connect.php';




if(isset($_POST["submit"])){

if(!empty($_POST['ename']) && !empty($_POST['pass'])) {
    $user=$_POST['ename'];
    $pass=$_POST['pass'];
    $securepass=md5($pass);



$query=mysql_query("SELECT * FROM users WHERE user_email='".$user."' AND user_pass='".$securepass."'");
    $numrows=mysql_num_rows($query);
    if($numrows!=0)
    {
    while($row=mysql_fetch_assoc($query))
    {
    $dbemail=$row['user_email'];
    $dbpassword=$row['user_pass'];
    $dbuser=$row['user_name'];
    }

    if($user == $dbemail && $securepass == $dbpassword)
    {

    session_start();
    $_SESSION['sess_user']=$dbuser;

    /* Redirect browser */
    header("Location: member.php");
    }
    } else {
    echo "Invalid username or password!";
    }

} else {
    echo "All fields are required!";
}
}
?>

but session is working don't know whey cookies isn't work

I told you why.

That one works does not automatically mean the other works too.

BTW, your session_start is better to have at the start too, and header redirects don't work after output either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.