0

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>
2
Contributors
5
Replies
17
Views
3 Years
Discussion Span
Last Post by pritaeas
0

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."

0

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!";
}
}
?>
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.