Cookie is not created and Page is not redirect properly

<?php
require_once('upper.php');
$error_msg='';
if(!isset($_COOKIE['LoginIdCookie'])){
if(isset($_POST['submit']))
{
require_once('database.php');
$LoginId=mysqli_real_escape_string($dbc,trim($_POST['LoginId']));
$Password=mysqli_real_escape_string($dbc,trim($_POST['Password']));
if((!empty($LoginId)) && (!empty($Password))){
$query="SELECT * FROM registration WHERE LoginId='$LoginId' AND Password=SHA('$Password') AND Flag='A'";
$result=mysqli_query($dbc,$query) or die('Not Connected');
if(mysqli_num_rows($result)==1)		
{
$row=mysqli_fetch_array($result);
setcookie('LoginIdCookie',$row['LoginId']);
//$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index1.php';
header('Location: index1.php');
}
else{
echo 'Enter right UserName/Password combination or You may not approved yet. ';
echo '<a href="ForgetPassword.php">Can\\'t access your account?"</a>';
}}
else{
echo' Fill all fields';
}
}}
?>
<html>
<title>Log In</title>
<body>
<?php
if(empty($_COOKIE['LoginIdCookie']))
{
echo $error_msg;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ;?>" >
<div class="LoginValidator">
<fieldset>
<legend>
Log In</legend><table><tr><td>
<label for="username">User Name :</label><br/><br/></td>
<td><input type="text" name="LoginId"><br/><br/></tr>
<td><label for="password">Password :</label></td>
<td><head>
   <div id="divMayus" style="visibility:hidden">Caps Lock is on.</div>
   <SCRIPT language=Javascript>

function capLock(e){
 kc = e.keyCode?e.keyCode:e.which;
 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
  document.getElementById('divMayus').style.visibility = 'visible';
 else
  document.getElementById('divMayus').style.visibility = 'hidden';
}

</SCRIPT>
   </HEAD>

<input onkeypress='return capLock(event)' type="password" name="Password"><br/></td></tr></table>
<input type="submit" name="submit" value="Login" class="Login_button">
</fieldset>

</div>
</form>

<?php 
}
else{
echo 'You are logged in as '.$_COOKIE['LoginIdCookie'];
}
require_once('lower.php');
mysqli_close($dbc);
?>



HI friends…
In above code when I enter wrong user name or password, it displays error msg properly but problem is that when i enter right user name and password instead to redirect to index1.php it displays LoginPage again…
I think cookie is not created as well…
I can’t understand where is problem???/
Plz help me…

Try this and see what happens

if (mysqli_num_rows($result) == 1)
{
    $row = mysqli_fetch_array($result);
    die('Hello '.$row['LoginId']);
}

Make sure to backup your current code

Sir,
My original code properly works on my local system i.e. in windows XP but when i upload it in my Linux Server it creates problem…
I don’t know what is wrong???////

Ohhhhh
I got the main problem…
Cookie is not setting in linux server actually but i don’t know why it is so???
Is there anyone???

You need to place start session above the code before you can set any cookies:


<?php
session_start();

//your code below

Also you can check if the cookie has been set by:


echo $_COOKIE["Name of the cookie"];

//or

print_r($_COOKIE); //to show all cookies

Sir, I run your code and I didn’t got ant cookie.
Are session_start(); is applicable for sessions and cookies both???
Anyway it also not works…
Any suggestion???/

Hello Frinds…
Plz give some suggestion on it.
Plzzzzzzzzz help…



<?php
[COLOR="Red"]session_start();//You place this session_start() here.[/COLOR]

require_once('upper.php');

$error_msg='';

[COLOR="Red"]if(isset($_COOKIE['LoginIdCookie']))[/COLOR]
{

if(isset($_POST['submit']))

{

require_once('database.php');

$LoginId=mysqli_real_escape_string($dbc,trim($_POST['LoginId']));

$Password=mysqli_real_escape_string($dbc,trim($_POST['Password']));

if((!empty($LoginId)) && (!empty($Password))){

$query="SELECT * FROM registration WHERE LoginId='$LoginId' AND Password=SHA('$Password') AND Flag='A'";

$result=mysqli_query($dbc,$query) or die('Not Connected');

if(mysqli_num_rows($result)==1)        

{

$row=mysqli_fetch_array($result);

setcookie('LoginIdCookie',$row['LoginId']);

//$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index1.php';

header('Location: index1.php');

}

else{

echo 'Enter right UserName/Password combination or You may not approved yet. ';

echo '<a href="ForgetPassword.php">Can\\'t access your account?"</a>';

}}

else{

echo' Fill all fields';

}

}}

?>

<html>

<title>Log In</title>

<body>

<?php

if(empty($_COOKIE['LoginIdCookie']))

{

echo $error_msg;

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ;?>" >

<div class="LoginValidator">

<fieldset>

<legend>

Log In</legend><table><tr><td>

<label for="username">User Name :</label><br/><br/></td>

<td><input type="text" name="LoginId"><br/><br/></tr>

<td><label for="password">Password :</label></td>

<td><head>

   <div id="divMayus" style="visibility:hidden">Caps Lock is on.</div>

   <SCRIPT language=Javascript>



function capLock(e){

 kc = e.keyCode?e.keyCode:e.which;

 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);

 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))

  document.getElementById('divMayus').style.visibility = 'visible';

 else

  document.getElementById('divMayus').style.visibility = 'hidden';

}



</SCRIPT>

   </HEAD>



<input onkeypress='return capLock(event)' type="password" name="Password"><br/></td></tr></table>

<input type="submit" name="submit" value="Login" class="Login_button">

</fieldset>



</div>

</form>



<?php 

}

else{

echo 'You are logged in as '.$_COOKIE['LoginIdCookie'];

}

require_once('lower.php');

mysqli_close($dbc);

?>



please make sure those changes and try again…
If is there any errors, let me know…

You don’t need session_start() for cookies, try this and if the cookie does have an issue an error should occur or a blank space will appear

if (mysqli_num_rows($result) == 1)
{
    $row = mysqli_fetch_array($result);
    setcookie('LoginIdCookie',$row['LoginId']);
    die('Hello '.$_COOKIE['LoginIdCookie']);
}

Are you sure this is true? since cookies can be read in subsequent page loads where sessions are not used.

Sir,
It displays just “Hello”.
What is problem sir…
Any idea???///

I think u need to chek every syntax…

But sir it properly works on windows xp i.e. my local computer…
So how syntax error is possible in that…

Lets try what [COLOR=#ff6600][B]spence_noodle[/B][/COLOR] said, by dumping all the cookies we will be able to see whats going on.

if (mysqli_num_rows($result) == 1)
{
    $row = mysqli_fetch_array($result);
    setcookie('LoginIdCookie',$row['LoginId']);
    echo '<pre>';
    print_r($_COOKIE);
    echo '</pre>';
    exit;
}

Just another tip.
Are the linux server is live?, i mean not your pc.
Maybe you need to mention path, just a guess.
Try this:

setcookie('LoginIdCookie',$row['LoginId'],'/');

If above doesnot work, try this:

setcookie('LoginIdCookie',$row['LoginId'],'/','website.com');

Change the website.com to your domain name.
Try it and let us know.

Is there an error log for PHP on the linux box?

Above Code displays “Array()”
Now what plz tell???///

Have you tried the other fixes mmarif4u posted

I don’t think that a cookie can be set if there is any prior output, and once set, it cannot be correctly read until the page has been reloaded.

Here’s what the setcookie() documentation has to say about it.

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction).

Once the cookies have been set, they can be accessed on the next page load

So, an effective solution is to check for byte-order markers and other potential output beforehand, and to ensure that the cookie is not attempted to be read in the same page load that it has been set.