<?php
// create array of movie posters,
// you can also get the list of posters from a database and loop over them the same way
$movie_poster[1] = ['image' => 'image_1.jpg', 'title' => 'Forest Gump'];
$movie_poster[2] = ['image' => 'image_2.jpg', 'title' => 'Lord of the Rings'];
$movie_poster[3] = ['image' => 'image_3.jpg', 'title' => 'Mission impossible'];
$movie_poster[4] = ['image' => 'image_4.jpg', 'title' => 'Indiana Jones'];
// check if user clicked on image
// for security purposes we check if variable is set then
// convert it to an integer. If it is not a number it will equal 0.
// This is for security purposes.
if (isset($_GET['id']) && (int)$_GET['id'] != 0) {
// user clicked so we display title of image
$id = (int)$_GET['id']; // convert to integer/number for security
?>
<h1><?php echo $movie_poster[$id]['title']; ?></h1>
<?php
}
// display the images on the page
foreach ($movie_poster as $id => $mp) {
?>
<div>
<a href="test.php?id=<?php echo $id; ?>">
<img src="<?php echo $mp['image']; ?>" />
<br /><?php echo $mp['title']; ?>
</a>
</div>
<?php
}
2条答案
按热度按时间nwlls2ji1#
如果用户只是点击一个链接,那么一般的方法是在该链接上放置一个查询字符串值。
例如:
然后在
movie.php
中,你会得到“123”的值:您可以使用该
$id
值查询数据库(或存储数据的任何地方),以获取所需的特定记录。cnwbcb6i2#
我已经为你创建了一个简单的演示你所要求的使用良好的php安全。你可以把我在这里所做的,并改善它,但你喜欢。我已经尽可能地简化了代码。
此代码的前提是基于安全性。对于这样的事情,总是在查询字符串中传递一个id /数字,并像这样访问数据:
test.php?id=3
我会让你的代码安全和易于管理。
创建一个名为test.php的文件并添加以下代码