skip to Main Content

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


  1. 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:

    <?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/' . $_GET['id'] . '" scrolling="no" width="622" height="350" frameborder="no" allowfullscreen></iframe>');
    }
    ?>
    

    Double quotes method:

    <?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/{$_GET['id']}' scrolling='no' width='622' height='350' frameborder='no' allowfullscreen></iframe>");
    }
    ?>
    

    note that you should sanitize $_GET[‘id’] before using it to avoid XSS attacks or other potential vulnerabilities.

    Login or Signup to reply.
  2. To include the value of $_GET['id'] inside the print 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:

    <?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/' . $_GET['id'] . '" 
        scrolling="no" width="622" height="350" frameborder="no" allowfullscreen></iframe>');
    }
    ?>
    

    In this code, $_GET['id'] is concatenated with the string using the '.' operator to include the value of the id parameter in the URL of the iframe source.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search