mkjwa
mkjwa

Reputation: 141

PHP setcookie is never setting

I'm trying to make a cookie. It doesn't make it.

This codes was working, but it doesn't work in recent system. login_check.html is the below.

<?
include "../_cfg/siteCfg.php"; //Site common values
include "$SITE_PATH/_class/dbms.php"; //mysql_class
include "$SITE_PATH/_class/Function.php";

$my_func = new PublicFunction();
$db = new mydb();   //DB Access

if ( $HTTP_POST_VARS[id] && $HTTP_POST_VARS[passwd] ) {
    $sql = "select uid, passwd, m_id, name, level from member_list where m_id = '$HTTP_POST_VARS[id]'";
    $result = $db->select($sql);

    if ( !$result ) {
        $my_func->virtual_function( "login_error ('ID_ERR')" );
    }
    else if ( $HTTP_POST_VARS[passwd] != $result[0][1] ) {
        $my_func->virtual_function( "login_error ('PW_ERR')" );
    }
    else if ( $result && ( $HTTP_POST_VARS[passwd] == $result[0][1] )) {
        $m_id = $result[0][m_id];
        $uid = $result[0][uid];
        $name = $result[0][name];
        $level = $result[0][level];

        setcookie("c_m_id",$m_id,0,'/');
        setcookie("c_uid",$uid,0,'/');
        setcookie("c_name",$name,0,'/');
        setcookie("c_level",$level,0,'/');

        $my_func->virtual_function("alert($_COOKIE[c_level])");  // This is for a log

    $my_func->virtual_function( "loginOk()" );
    }
} else {
    $my_func->virtual_function("alert('Bad Access!!!')");
}

?>

When I make a alert to check COOKIE Values, It's null.

And I used var_dump(isset($_COOKIE[c_uid]));, ant then it returns bool(false) .

Plus I changed header @header('P3P: CP="ALL CURa ADMa DEVa TAIa OUR BUS IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC OTC"'); to header('P3P: CP="NOI CURa ADMa DEVa TAIa OUR DELa BUS IND PHY ONL UNI COM NAV INT DEM PRE"'); .

This also not worked.

This code is called by the below. The above code is login.html

<?
    require_once "$DOCUMENT_ROOT/_cfg/siteCfg.php"; //site value
    require_once "$SITE_PATH/_class/htmlHead.php";

    $title = "Login"; //title value
    $htmlTPL = new htmlTPL($title); //HEAD Instance creation
?>
<title></title>
<link href="<?=$SITE_HOME?>/_styles/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<?=$SITE_HOME?>/_js/common.js"></script>
<script>
//<!--
    function login_check(){
        var fm = document.login_form;

        fm.target = "vr_url";
        fm.action = "./login_check.html";
        fm.submit();
    }
    function login_error ( num ) {
        fm = document.login_form;
        if ( num == "ID_ERR" ) {
            alert ( "Not registered user.");
            fm.passwd.value = "";
            fm.id.focus();
            fm.id.select();
        } else if ( num == "PW_ERR" ) {
            alert ( "Wrond Pasword." );
            fm.passwd.focus();
            fm.passwd.select();
        }
    }

    function loginOk(){
        opener.location.reload(true);
        window.close();
    }
//-->
</script>

This is a part of siteCfg.php

<?
//@header("Cache-Control: no-cache,must-revalidate");
//@header ("Pragma: no-cache");
@header('P3P: CP="ALL CURa ADMa DEVa TAIa OUR BUS IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC OTC"');

//header('P3P: CP="NOI CURa ADMa DEVa TAIa OUR DELa BUS IND PHY ONL UNI COM NAV INT DEM PRE"'); 
//error_reporting(E_ALL); 

$SITE_PATH  = $DOCUMENT_ROOT;
$IMG_PATH = $SITE_PATH."/image";
$MANAGER_PATH = $SITE_PATH."/manager";


$SITE_HOME  = 'http://www.mysite.com';
$MANAGER_HOME = $SITE_HOME."/manager";
$IMAGE_HOME = $SITE_HOME."/image";
$ADMIN_HOME = $SITE_HOME."/manager_admin";

//Login Return URL
$PREURL = urlencode($SITE_HOME.$_SERVER["REQUEST_URI"]);

//Values Setting

$_name = $_COOKIE[c_name];
$_mid = $_COOKIE[c_m_id];
$_uid = $_COOKIE[c_uid];
$_mlevel = $_COOKIE[c_level];

//var_dump(isset($_name));      

//FTP Setting

PHP Version 5.2.17 . Is therea anything I can check to solve this problem. Please, tell me. I tested on IE 11, Chrome 30.0.1599.101 m , Firefox, Safari. not working at all

Upvotes: 1

Views: 397

Answers (2)

Daniel W.
Daniel W.

Reputation: 32260

You have a blankspace at the beginning of your script. Cookies won't be set if you have HTTP body before it.

See:

 <?
^-----
include "../_cfg/siteCfg.php"; //Site common values
incl....

Also your coding style can be much improved. Not using short-open-tags for example and putting single-quotes arround string index: $varRS['id'].

This won't work:

virtual_function("alert($_COOKIE[c_level])");

you need to properly concatenate variables and use quotes arround strings:

virtual_function("alert(" . $_COOKIE['c_level'] . ")");

If you don't see any error messages, make sure you have error reporting turned on. http://blog.flowl.info/2013/enable-display-php-errors/

Upvotes: 1

Eike Pierstorff
Eike Pierstorff

Reputation: 32760

You might try to convert $HTTP_POST_VARS to $_POST (since $HTTP_POST_VARS is deprecated and I'm not even sure it's still supported with PHP > 5, which might be very well your issue if it stopped working on a new system).

Upvotes: 0

Related Questions