2

Im following a MySQL article on the basics of using HTML and PHP. The two files I have are very very basic:

Index.html:

<A HREF="welcome.php?name=Kevin"> Hi, I'm Kevin! </A>

welcome.php:

<?php
  echo( "Welcome to our Web site, $name!" );
?>

When the link is clicked on the index page, I get the following error:

Notice: Undefined variable: name in C:\xampp\htdocs\mywebsite\welcome.php on line 2
Welcome to our Web site, !

Im not exactly an expert but i pretty sure the codes correct - is there any sort of setting/configuration that I shouldve completed before trying to run it which is preventing the variable being passed to the php file?

Thanks, Gio

4

2 回答 2

2

在您的示例中,您正在使用 PHP 的全局变量功能,该功能已被弃用,因为安全性。

您可以使用 $_GET 获取 URL 变量

<?php
  $name = $_GET['name']; // Gets ?name=value
?>

您使用 $_POST 从通过 POST 提交的表单中检索数据

<?php
  $name = $_POST['name']; // Get <input name="name"> value via POST
?>

您使用 $_REQUEST 根据当前请求方法从 GET 或 POST 检索数据

<?php
  $name = $_REQUEST['name'];
?>
于 2013-11-04T23:44:17.633 回答
2

为此,您需要使用$_GET从 URL 中提取“名称”参数。

<?php
  $name = $_GET['name'];
  echo( "Welcome to our Web site, $name!" );
?>

您还需要考虑使用 等方法清理输入以防止代码注入htmlspecialchars(),并首先使用 . 检查 URL 是否包含“名称”参数isset()

<?php
    if (isset($_GET['name']) {
        $name = htmlspecialchars($_GET['name']);
        echo( "Welcome to our Web site, $name!" );
    }
?>
于 2013-11-04T23:35:56.383 回答