How to get image URL to display in PHP - php

Can't get image URL to display in my code; this is my URL: https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!DOCTYPE html>
<html>
<head>
<title>PHP Exercise 1: Links and Variables</title>
</head>
<body>
<h1>PHP Exercise 1: Links and Variables</h1>
<p>Use PHP echo and variables to output the
following link information:</p>
<hr>
<?php
$linkName ='<h1> Codecademy <h1>';
$linkURL = 'codecademy';
$linkImage =
'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
$linkDescription = 'Learn to code interactively, for free.';
$my_name = "peter";
echo "<img>" . $linkImage . "</img>";
echo $linkName;
echo "<br>";
echo $linkURL;
echo "<br>";
echo $linkImage;
echo "<br>";
echo $linkDescription;
$linkImage = 'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
echo '<img src="data:image/jpeg;base64,">';
?>
</body>
</html>

A link to an image should always be put in the src attribute when you're using the img tag in html.
That means you should be looking for something like "<img src=".$linkImage."></img>" instead of "<img>".$linkImage."</img>"
Of course, that only applies if the problem you're referring to is the fact that there should be an image after the sentence "Learn to code interactively, for free."

Works just fine for me. Try checking php permissions.

Related

Hide Div From those who visit directly to the page but don't who refer from a exact domain using php

I want to hide a div from direct users but showing the same div to those who come from example.com
eg.
example123.com/article.php have below div
<div id="main">Title</div>
(when user click on a hyperlink on example.com
Artile
then show the above div
but when user come directly to example123.com/article.php then don't show the div.
how will I do that using php?
Hi you can use the following code like this.
<?php if (isset($_SERVER['HTTP_REFERER'])){ ?>
<div style="width:200px;height:200px;border:1px solid black">
<?php } ?>
You need to make use of $_SERVER['HTTP_REFERER']
I don't totally follow your question but this code should be enough for you to adapt to your needs:
if(strstr($_SERVER['HTTP_REFERER'],'example.com')) {
echo '<div id="main">Title</div>';
}
So, if the referrer URL contains example.com then echo your div.
If the referral URL didn't contain example.com or was empty (i.e. they arrived directly at your site) then the div won't show.
You can achieve this by passing an argument from the URL. The value of the argument will be null if they access the page directly and only have a value if they use the specific URL. Then your PHP can just check the argument and handle it accordingly.
Example as follows.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 01</h1>
Regular URL
<br />
Argument URL
</body>
</html>
Then you can handle the Arguments in your PHP page containing the div
pagewithdiv.php
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<div id="conditional">
<h2>Conditional Div</h2>
</div>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "block"';
echo '</script>';
} else {
echo '<script>';
echo 'document.getElementById("conditional").style.display = "none"';
echo '</script>';
}
?>
</body>
</html>
Keep in mind though that this is only hiding the div, it still exists on the page. If you want it to be completely gone then instead of using javascript to change the visibility you can echo the code that makes up the div if the requirements are met.
<!DOCTYPE html>
<html>
<head>
<title>Nothing</title>
</head>
<body>
<h1>Nothing 02</h1>
<?php
if (
// check if argument exists
isset($_GET["Condition"])
&&
// check if argument value is true
trim($_GET["Condition"] == true)
) {
echo '<div id="conditional">';
echo ' <h2>Conditional Div</h2>';
echo '</div>';
}
?>
</body>
</html>

passing variables in a php link to another page with auto scroll

and thank you in advance for your great work here, i need help with some coding, i got a web page where i show a list of records of a mysql table, with a hyperlink when you click on it creates a variable like world.php?id=1&xx=1050&yy=300 so when the user clicks it takes him to next page and scroll down and up to the x and y it, works fine in web browsers but not working in mobiles. can you help me? the page world.php has
,)">
this is the code i'm using
echo '<a href="world.php?xx='. $_SESSION['x'] .'&yy='. $_SESSION['y'] .'">';
echo '<b>';
echo $row['usuario_atacante'];
echo '</b>';
echo '</a>';
Can you do something like this ?
Row 5
and make links in your world.php file.
<table>
<tr id="row1"><td><h2>Row 1</h2></td></tr>
<tr id="row2"><td><h2>Row 2</h2></td></tr>
<tr id="row3"><td><h2>Row 3</h2></td></tr>
<tr id="row4"><td><h2>Row 4</h2></td></tr>
<tr id="row5"><td><h2>Row 5</h2></td></tr>
</table>
Use a link like: world.php?x=345&y=200 to scroll to that location.
<?php
if(isset($_GET["x"]) && isset($_GET["y"])) {
$x = $_GET["x"];
$y = $_GET["y"];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>World Map</title>
<script>
function scrollWin() {
window.scrollTo(<?php echo $x.",".$y; ?>);
}
</script>
</head>
<body onload="scrollWin()">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e2/Geoffroy's_Cat.jpg" />
</body>
</html>
This is just a example to get you started.

How to echo a string which contains <?php ?>

I want to generate a php page which contains HTML and some php commands. Problem is that when I submit the code below, I get this output
<?php echo '$stfromyearErr '?><?php echo '$stfrommonthErr '?><?php echo '$stfromdayErr '?> <?php echo '$sttoyearErr '?><?php echo '$sttomonthErr '?><?php echo '$sttodayErr '?>
rather than php commands. How do I fix this??
Thanks!!
<?php
$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='0' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo '\$stfromyearErr ';?&gt&lt?php echo '\$stfrommonthErr
';?&gt&lt?php echo '\$stfromdayErr ';?&gt &lt?php echo '\$sttoyearErr ';?
&gt&lt?php echo '\$sttomonthErr ';?&gt&lt?php echo '\$sttodayErr ';?&gt</span></td>
</tr></table> </body></html>";
echo $page;
?>
You're echoing out your PHP tags as <?php, and then presumably displaying that in a browser. It will LOOK like php code in a browser, because the browsers will render < as < but it's NOT PHP code. it's just some text.
PHP is not recursively executable, e.g.
<?php
echo "<?php echo 'foo '; ?>";
?>
would echo out <, ?, p, etc..., not just foo.
You can do stuff like
<?php
$foo = "<?php echo 'hello world!'; ?>";
file_put_contents('hello.php', $foo);
?>
without any issues. As long as the file you're producing actually gets executed by PHP (e.g. don't name it "hello.html"), then PHP will not know (or even care) that the script was produced by some OTHER php code.
You are encapsulating your variables in single-quotes, therefore they get output as plain text, simply remove the commas on any of your echo statements to fix this.
<?php echo $stfromyearErr; ?>
When you want to output PHP code including tag and even syntax highlighting, you can use the function highlight_string(). Documentation here
<?php
highlight_string("<?php echo '$stfromyearErr '?><?php echo '$stfrommonthErr '?><?php echo '$stfromdayErr '?> <?php echo '$sttoyearErr '?><?php echo '$sttomonthErr '?><?php echo '$sttodayErr '?>");
?>
There are two things, first remove the quotes:
<?php
$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='1' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo $stfromyearErr ?&gt&lt?php echo $stfrommonthErr
?&gt&lt?php echo $stfromdayErr ?&gt &lt?php echo $sttoyearErr ?
&gt&lt?php echo $sttomonthErr ?&gt&lt?php echo $sttodayErr ?&gt</span></td>
</tr></table> </body></html>";
echo $page;
?>
However, this will give you:
<?php echo your_var_1 ?><?php echo your_var_2 ?><?php echo your_var_3 ?> <?php echo your_var_4 ? ><?php echo your_var_5 ?><?php echo your_var_6 ?>
This is because the php tags are redundant: you have already started php and are echoing these out within php.
If this is unavoidable, try replacing them like this:
echo str_replace(" ?&gt", "", str_replace("&lt?php echo", "", $page));

Include php file in html page

I'm working with XML and PHP to populate a web form drop down box.
I have a html page
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<header>
<h1>Title</h1>
</header>
<form>
<div id ="select xml">
<?php include 'dropdown.php'; ?>
</div>
</form>
</body>
</html>
and I am hoping to include the following PHP to generate the actual box with the file names of the XML.
<?php
//echo(substr(glob("xml/*.xml")[0],4));
echo "<p>
<label>Select list</label><br>
<select id = \"selectxml\">
<option value'0'>--Please Select--</option>";
$count = count(glob("xml/*.xml"));
$files = glob("xml/*.xml");
for ($i = 0; $i < $count; $i++) {
//echo(substr($files[$i],4));
//echo "<br>";
$filename = (substr($files[$i],4));
echo "<option value=$i+1>$filename</option>";
}
echo "</select><br>
<br>
</p>";
?>
I'm aware the PHP isnt perfect, but it works.
The issue is that the Include HTML does not work when I run the page - any ideas?
Change the extension to .php and you will be okay .
When the page have .html extension, the web server doesn't recognize it as a PHP file, and you can't use PHP codes in it. and any PHP code, will be processed as a plain text .
I mean when you put :
<?php echo "hello" ?>
in a HMTL page, browser will show :
<?php echo "hello" ?>
But, if the page have .php extension, browser will show :
hello
Rename your html page from .html to .php
it;s impossible, but with jQuery.load function you can do this :
$("#select-xml").load("dropdown.php");

php inside <a> of HTML

I am a beginner and trying to use PHP statement inside <a> tag of HTML. I don't know whether it is possible or not and I tried to search it on Google, but I couldn't find any answer. Below is the code I am trying to execute.
Whenever I run this code, I do not get any error but the my browser does not display the value of $link1, $link2 and $link3 which I put inside the <a> tag of HTML.
I saved the document as index.php
<?php
$title = 'Shellitic';
$link1 = 'Home';
$link2 = 'Contact';
$link3 = 'About';
?>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1> Welcome to <?php echo $title; ?> </h1>
<p></br></p>
<p>To visit our home page, click on the <?php $link1; ?> button</p>
<p>To visit our contact page, click on the <?php $link2; ?> button</p>
<p>To visit our About page, click on the <?php $link3; ?> button</p>
</body>
You need to echo the values:-
<?php echo $link1; ?>
Some servers may accept this
<?= $link1; ?>
But it is typically safer to use
<?php echo $link1; ?>
Add echo command.
Like this:
<?php echo $link1; ?>

Categories