设置cookie获取未定义索引的错误

时间:2017-09-22 05:28:00

标签: php html

<script>
	$(document).ready(function(){	
	setTimeout(function(){ 
	if(!mag_cookie_val)
		$("#myNav").show(); 
	}, 12000);		
	$("#close_magnet").click(function(){
		$("#myNav").hide(); 
	});
	$("#magnet_email").keyup(function(){
		$("#magnet_email").css("border-color","#67AEE9");   	
	});

	$("#magnet_continue").click(function(){
		var MagEmail = $("#magnet_email").val();
		atpos = MagEmail.indexOf("@");
		dotpos = MagEmail.lastIndexOf(".");
		if(atpos < 1 || ( dotpos - atpos < 2 )) 
		{
		   $("#magnet_email").css("border-color","red");                   
		}else{
			/*
			$.ajax({
			  url: "leadmagnet_res.php",
			  method: 'POST',
			  data:{magemail:MagEmail}
			}).done(function(data) {
			 //alert(data)
			  if(data !=0){
				  $("#myNav").css("height","0%"); 
			  }
			});
			*/
			$.ajax({
				url: 'leadmagnet_res.php',
				type: 'post',
				data: {magemail:MagEmail},
				//processData: false,
				success: function( res ){
					//alert(res);						
				},
				error: function(){
					alert( "error occured" );
				},
				complete: function(){
					$("#myNav").hide();
				}
			});
		}
	});


//Lead magnet close code End
$(document).ready(function() {
	
//setTimeout(function(){ $("#myNav").show(); }, 12000);
$("#magnet_email").css("border-color","#67AEE9"); 	
//Lead magnet close code start	
$("#close_magnet").click(function(){
$("#myNav").css("height","0%"); 
});

$("#magnet_email").keyup(function(){
	
$("#magnet_email").css("border-color","#67AEE9");   	

});

$("#magnet_continue").click(function(){
var MagEmail = $("#magnet_email").val();
atpos = MagEmail.indexOf("@");
dotpos = MagEmail.lastIndexOf(".");
if(atpos < 1 || ( dotpos - atpos < 2 )) 
{
//alert(1);
   $("#magnet_email").css("border-color","red");         
           
}else
{
	//alert(2);	 
$.ajax({
   url: siturl_mag+"leadmagnet_res.php",
  method: 'POST',
  data:{magemail:MagEmail}
}).done(function(data) {
 //alert(data)
  if(data !=0){
	  $("#myNav").css("height","0%"); 
  }
  
});
			 
}
	
});

//Lead magnet close code End
	</script>		
			<style>

.overlay_magnet {
    height: 0% !important;
    width: 100% !important;
    position: fixed !important;
    z-index: 94 !important;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.3);
    transition: 0.5s;
}

.overlay_magnet-content {
    margin-left: 27%;
    margin-top: 30px;
    position: relative;
    text-align: center;
    top: 0;
    width: 45%;
}



.overlay_magnet a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay_magnet a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay_magnet .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay_magnet {}
  .overlay_magnet a {font-size: 20px}
  .overlay_magnet .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}
.circle_magnet {
    width: 100%;
    padding: 10%;
    background: white;
    border-radius: 50px;
    border-style: solid;
    border-color: #FE0000;
    border-width:4px;
}
.circle_magnet-wrapper {
    margin: 0 5%;
    position: relative;
}
.circle_magnet .list-inline {
    font-size: 0px;
    margin: 0;
}
.circle_magnet .list-inline li {
    font-size: 20px;
    display: block;
}
.circle_magnet p {
    padding-top: 10%;
    margin: 0;
}
@media (min-width:460px) { 
	.circle_magnet .list-inline li {
	    font-size: 50px;
	    display: inline-block;
	}
	.circle_magnet-wrapper {
	    width: 100%;
	    position: relative;
        margin:0 auto;
	}
	.circle_magnet {
	    height: 0px;
	    padding: 50%;
	    border-radius: 50%;
	}
	.circle_magnet > div {
	    position: absolute;
	    text-align: center;
	    left: 10%;
	    right: 10%;
	    top: 50%;
	    -webkit-transform: translateY(-50%);
	    -ms-transform: translateY(-50%);
	    transform: translateY(-50%);
        top:20%\9; /* ie8 hack test this out it's a guess */
	}
}

.circle_text_magnet{
cursor:pointer;	
	
}

.circle_text_magnet:hover {
   text-decoration: underline;
}

   .arrow{
    height:25px;
    width:25px;
  }


</style>
<div id="myNav" class="overlay_magnet hidden-xs hidden-sm" <?php if ($magstatus !=0){?>style="height:0% !important;display:none;" <?php }else{ ?>style="height:100% !important;display:none;" <?php } ?> >
<div class="overlay_magnet-content" >
<div class="circle_magnet-wrapper">
<div class="circle_magnet text-center">
<div class="row" style="margin-top:-40%;">
    <div class="col-md-12"> 
		<center>
		<label style="font-family: Roboto;color: green;text-shadow: rgb(46, 46, 46) 0px 4px 5px;font-size: 42px;font-weight: bold !important;margin: 0 !important;">Sign Up!</label>
		
		</center>
	</div> 
	</div>
   
	<div class="row" style="margin-top:-24%;">
    <div class="col-md-12"> 
		<center>
		<label style="font-family: Roboto;color: #00006A;font-size: 20px;font-weight: normal !important;margin: 0 !important;">Join our Travel Community.</label>
		<label style="font-family: Roboto;color: #00006A;font-size: 20px;font-weight: normal !important;margin: 0 !important;">Get Free Exclusive Travel Tips and<br> Links to our New Travel Videos.</label>
		</center>
	</div> 
	</div>
	
	
	<div class="row" style="margin-top:-12%;">
    <div class="col-md-12"> 
		<center>
		
		<label style="font-family: Roboto;color: #fe0000;font-size: 20px;font-weight: normal !important;margin: 0 !important;">Enter your email below</label>

				</center>
	</div> 
	</div>
	
<div class="row" >
<div class="col-md-12" style="border: 1px solid #00006A;border-radius: 7px;padding: 18px;box-shadow: inset 0px 0px 50px 0px #ABABAB, 0px 2px 9px 1px #242424;">

		<div class="col-md-9"> 	 
 <input id="magnet_email" class="form-control" placeholder="Enter your email address " type="text"  > 
	</div>
	
<div class="col-md-3" >	 
<button  class="btn btn-primary pull-left " id="magnet_continue">Continue</button>&nbsp; 
</div>

</div>
</div>

<div class="row" style="margin-top:20%;">
    <div class="col-md-12"> 
	
		<span style="font-family: Roboto;color: #00006A;font-size: 12px;font-weight: normal !important; ">By clicking Continue, I agree to the </span>
		<span><a href="<?php echo $urbtn;?>hipfig-terms-of-use" style="font-size:12px;font-family: Roboto;color: #fe0000;"> Terms of Service</a></span>
			
	</div> 
	</div>






        <div class="row" style="margin-top:35%;">
    <div class="col-md-12"> 
		<center><h2 id="close_magnet" class="circle_text_magnet" style="font-family: Roboto;color: #00006A;font-weight: normal !important; margin-left:10px; ">No Thanks</h2></center>
		</div> 
   </div>

      
   </div>
    
</div>

</div>
  

这是我为要显示的表单设置的Cookie代码

但总是收到未定义错误

  

索引:第3行的MagnetStatus

$magstatus = $_COOKIE[$cookie_name];

代码:
    

$magstatus = $_COOKIE[$cookie_name];

if(!$magstatus) {

$cookie_value = 0;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
}

?>

所以你可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

如果具有指定名称的Cookie不存在,您可以使用isset()empty()来阻止此类警告

$magstatus = (isset($_COOKIE[$cookie_name])) ? $_COOKIE[$cookie_name] : 'default_value_here';

答案 1 :(得分:0)

您必须使用以下代码来检查cookie是否已设置:

if(!isset($_COOKIE[$cookie_name])) {

$cookie_value = 0;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
}

仅使用$magstatus = $_COOKIE[$cookie_name];方法

删除isset行检查Cookie