i want to hide an iframe for direct access.
and this code works fine
<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match('~mysite\.com$~', parse_url($referrer, PHP_URL_HOST))) {
print ('<iframe src="https://www.dailymotion.com/embed/video/"
scrolling="no" width="622" height="350" frameborder="no" allowfullscreen</iframe>">');
} ?>
but when i try to add <?php echo $_GET['id']; ?>
in iframe url
<?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match('~mysite\.com$~', parse_url($referrer, PHP_URL_HOST))) {
print ('<iframe src="https://www.dailymotion.com/embed/video/<?php echo $_GET['id']; ?>"
scrolling="no" width="622" height="350" frameborder="no" allowfullscreen</iframe>">');
} ?>
i got an error
Parse error: syntax error, unexpected 'id' (T_STRING) in /www/wwwroot/domain.com/index.php on line 4
any solution ?
2
Answers
In PHP, you can’t nest tags. You’re trying to include a PHP echo statement within a print statement, which is incorrect.
You can solve this problem by concatenating the variable to the string, or use double quotes ("") and include the variable directly. Here’s an example of both approaches:
Concatenation method:
Double quotes method:
note that you should sanitize $_GET[‘id’] before using it to avoid XSS attacks or other potential vulnerabilities.
To include the value of
$_GET['id']
inside theprint
statement, you don’t need to use the<?php ?>
tags again because you are already inside the PHP code block. You can directly concatenate the variable with the string using the'.'
operator. Here’s the corrected code:In this code,
$_GET['id']
is concatenated with the string using the'.'
operator to include the value of theid
parameter in the URL of theiframe
source.