Solved

PHP embed <script> tag into if else statement

Posted on 2013-06-10
12
348 Views
Last Modified: 2013-09-17
Hi,

I have this code:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "***********";
}
else
{
echo"";
}
?>

end code.


When ever I try to insert this code where the stars are now I get an unexpected < error,


<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>

Any ideas??

Thanks
0
Comment
Question by:tonypearce
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 30

Expert Comment

by:Randy Downs
ID: 39234711
If you are inserting into an existing  block of php code you don't need these tags <?php ?>

In other words something like this

<?php 
some code

<?php 
more code
?>

?>

Open in new window

0
 
LVL 30

Expert Comment

by:Randy Downs
ID: 39234721
This is how you would insert the code in the above scenario


<?php
some code


$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "***********";
}
else
{
echo"";
}


?>
0
 
LVL 12

Expert Comment

by:adrian_brooks
ID: 39234724
First of all, the use of eregi has been deprecated since PHP 5.3 and its continued use is highly discouraged. http://us2.php.net/manual/en/function.eregi.php

I would consider the use of preg_match as a replacement to the use of eregi.

Secondly, your supplied code sample doesn't provide enough detail to effectively help you debug the issue.

~AB
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 30

Expert Comment

by:Randy Downs
ID: 39234744
After re-reading this is probably what you need:

http://php.about.com/od/learnphp/qt/php_with_html.htm

<?php
 //your php code here
 ?>

 <b>Here is some more HTML</b>

 <?php
 //more php code
 ?>

 
 </body>
 </html>

Open in new window

0
 

Author Comment

by:tonypearce
ID: 39234760
Hi,

Sorry I have made myself very unclear..

This is what I'm trying to do and the code as I have tried:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>";
}
else
{
echo"";
}
?>

This code is supposed to show some code if a certain url is used,
0
 
LVL 30

Expert Comment

by:Randy Downs
ID: 39234787
Try this:

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
?>
<script src="//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js"></script>

<?php
}
else
{
echo"";
}
?>
0
 
LVL 12

Expert Comment

by:adrian_brooks
ID: 39234795
Then what you actually want would be this;

<?php
$str = $_SERVER['REQUEST_URI'];
if (eregi('\route=checkout/cart', $str)) {
echo "<script src=\"http://s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js\"></script>";
}
else
{
echo"";
}
?>

Open in new window


Please take note of the escaped double quotes in my code revision. \"

This is required, because without that, PHP will see your string end at the next unescaped double quote.

~AB
0
 

Author Comment

by:tonypearce
ID: 39234936
Hi,

Thanks, but still getting this error:
Parse error: syntax error, unexpected '<' in **************

any ideas?
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39234952
Avoid propagating any use of ereg() functions.

Please use view-source on these links.
see http://www.laprbass.com/RAY_temp_tonypearce.php 
and http://www.laprbass.com/RAY_temp_tonypearce.php?route=checkout/cart

<?php // RAY_temp_tonypearce.php
error_reporting(E_ALL);

// CREATE A REGULAR EXPRESSION
$rgx 
= '#'                     // REGEX DELIMITER
. 'route=checkout/cart'   // LITERAL STRING
. '#'                     // REGEX DELIMITER
;

// IF AND ONLY IF THE EXPRESSION MATCHES THE URL
if (preg_match($rgx, $_SERVER['REQUEST_URI'])) 
{
    echo "<script src=\"http://s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js\"></script>";
}

Open in new window

HTH, ~Ray
0
 
LVL 82

Expert Comment

by:hielo
ID: 39236665
Try:
<?php
$str = $_SERVER['REQUEST_URI'];
if (preg_match('#\broute=checkout/cart#', $str))
{
	echo "<script src='//s3.amazonaws.com/searchdiscovery-satellite-production/1234567897.js' type='text/javascript'></script>";
}
else
{
	echo "";
}
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39237440
@hielo: What do you get if you run this script ;-)
echo "";

Open in new window

Just kidding, ~Ray
0
 
LVL 82

Expert Comment

by:hielo
ID: 39238136
Well Mr. Paseur, if you can't figure that out a refresher may be necessary.

Allow me to refer you to:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23681009.html#a22325869

:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question