Solved

PHP embed <script> tag into if else statement

Posted on 2013-06-10
12
327 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
  • 4
  • 2
  • 2
  • +2
12 Comments
 
LVL 29

Expert Comment

by:Randy Downs
Comment Utility
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 29

Expert Comment

by:Randy Downs
Comment Utility
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
Comment Utility
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
 
LVL 29

Expert Comment

by:Randy Downs
Comment Utility
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
Comment Utility
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 29

Expert Comment

by:Randy Downs
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Expert Comment

by:adrian_brooks
Comment Utility
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
Comment Utility
Hi,

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

any ideas?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now