此Cookie可在Localhost上使用,但不能在主站点上使用。 救命!!伙计们 我尝试了所有事情并做了一切,但仍然没有用。我到处搜索此问题,却一无所获。因此,我在这里张贴了文章,以便我可以解决此问题。此外,是否还有其他简单的方法或技巧这个具体问题?
<?php
$connect = new PDO("mysql:host=localhost;dbname=pramit", "root", "");
$message = '';
if(isset($_POST["add_to_favourite"]))
{
if(isset($_COOKIE["event_c"]))
{
$cookie_data = stripslashes($_COOKIE['event_c']);
$event_data = json_decode($cookie_data, true);
}
else
{
$event_data = array();
}
$event_id_list = array_column($event_data, 'event_id');
if(in_array($_POST["hidden_id"], $event_id_list))
{
foreach($event_data as $keys => $values)
{
if($event_data[$keys]["event_id"] == $_POST["hidden_id"])
{
$event_data[$keys]["event_quantity"] = $event_data[$keys]["event_quantity"] + $_POST["quantity"];
}
}
}
else
{
$event_array = array(
'event_id' => $_POST["hidden_id"],
'event_name' => $_POST["hidden_name"],
'event_date' => $_POST["hidden_date"],
'event_quantity' => $_POST["quantity"]
);
$event_data[] = $event_array;
}
$event_data = json_encode($event_data);
setcookie('event_c', $event_data, time() + (86400 * 30));
header("location:index.php?success=1");
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
$cookie_data = stripslashes($_COOKIE['event_c']);
$event_data = json_decode($cookie_data, true);
foreach($event_data as $keys => $values)
{
if($event_data[$keys]['event_id'] == $_GET["id"])
{
unset($event_data[$keys]);
$event_data = json_encode($event_data);
setcookie("event_c", $event_data, time() + (86400 * 30));
header("location:index.php?remove=1");
}
}
}
if($_GET["action"] == "clear")
{
setcookie("event_c", "", time() - 3600);
header("location:index.php?clearall=1");
}
}
if(isset($_GET["success"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Event Added to favourite
</div>
';
}
if(isset($_GET["remove"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Event Removed
</div>
';
}
if(isset($_GET["clearall"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Your Favourite is cleared....You are free to go..........
</div>
';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Event Favourite</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<br />
<h3 align="center">Event Favourite</h3><br />
<br /><br />
<?php
$query = "SELECT * FROM event_favourite ORDER BY id ASC";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
?>
<div class="col-md-3">
<form method="post">
<div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px;" align="center">
<img src="images/<?php echo $row["image"]; ?>" class="img-responsive" /><br />
<h4 class="text-info"><?php echo $row["name"]; ?></h4>
<h4 class="text-danger"> <?php echo $row["date"]; ?></h4>
<!-- <input type="text" name="quantity" value="1" class="form-control" />-->
<input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />
<input type="hidden" name="hidden_date" value="<?php echo $row["date"]; ?>" />
<input type="hidden" name="hidden_id" value="<?php echo $row["id"]; ?>" />
<input type="submit" name="add_to_favourite" style="margin-top:5px;" class="btn btn-success" value="Add to favourite" />
</div>
</form>
</div>
<?php
}
?>
<div style="clear:both"></div>
<br />
<h3>Event Details</h3>
<div class="table-responsive">
<?php echo $message; ?>
<div align="right">
<a href="index.php?action=clear"><b>Clear Event</b></a>
</div>
<table class="table table-bordered">
<tr>
<th width="40%">Event Name</th>
<th width="1%">---</th>
<th width="20%">date</th>
<th width="1%">----</th>
<th width="5%">Action</th>
</tr>
<?php
if(isset($_COOKIE["event_c"]))
{
$total = 0;
$cookie_data = stripslashes($_COOKIE['event_c']);
$event_data = json_decode($cookie_data, true);
foreach($event_data as $keys => $values)
{
?>
<tr>
<td><?php echo $values["event_name"]; ?></td>
<td><?php echo $values["event_quantity"]; ?></td>
<td> <?php echo $values["event_date"]; ?></td>
<td> <?php// echo number_format($values["event_quantity"] * $values["event_date"], 2);?></td>
<td><a href="index.php?action=delete&id=<?php echo $values["event_id"]; ?>"><span class="text-danger">Remove</span></a></td>
</tr>
<?php
//$total = $total + ($values["event_quantity"] * $values["event_date"]);
}
?>
<tr>
<td colspan="3" align="right"></td>
<td align="right"> <?php// echo number_format($total, 2); ?></td>
<td></td>
</tr>
<?php
}
else
{
echo '
<tr>
<td colspan="5" align="center">No Event Added to favourite</td>
</tr>
';
}
?>