Solved

PHP embed <script> tag into if else statement

Posted on 2013-06-10
12
335 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
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 29

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 29

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 29

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 109

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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 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