How To: PHP elseIf to change div depending on the URL Contents - php

Issue: I have a php file that I do not have access to but I need to be able to format it. It generates several different pages depending on the search. I have been able to change a div using php if else statements in the header that the php calls to. (See Below)
What I would like to do is have a statement that will change the div depending on what is in the url, now the issue with this is that one of the pages I need to change, has the "submitIt" in the url which is what I am using to change the first div.
I have tried to change the code so that it calls to a different portion of the url but because the url still contains the first bit "submitIt" it calls to that div, rather than the new div.
Is there a way to call the elseif statement where if it contains "submitIt and "MEDIA_TYPE_ID" then it would print out the new div?
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "submitIt")) {
echo '<div style="width:560px;" class="app-h3 app-table">';
}elseif (strpos($url, "MEDIA_TYPE_ID")) {
echo '<div style="width:560px;" class="app-h3 app-table-none">';
}else {
echo '<div style="width:560px;" class="app-h3">';
}
?>

I think you are looking for the && operator. It means 'and'.
if (strpos($url, "submitIt") && strpos($url, "MEDIA_TYPE_ID")) {
// do something
}
If both of those are true, it will execute the code block.

Related

Want to use PHP include to place a page header (in Bootstrap 5) based on the file name (and location)

I want to place an image, HTML or simply text as a page header on multiple pages, but it is specific to each page, based on the file name (and folder it is in).
So, for example, domain.com/portfolio/index.php gets one image (or text/HTML/CSS), domain.com/portfolio/about/index.php gets another, domain.com/portfolio/contact/index.php gets another and so on.
Basically, I want to update this common element from one file instead of updating a bunch of files. I will usually use either the same image or the same HTML/CC design with different text and/or image in it, so the code example below includes a simplified version of each, just in case.
I've successfully used this in the past for pageheaders on sites but it no longer seems to work (PHP updated or maybe Bootstrap 5 is mucking things up)... it sits in pageheader.php which is then included in the top.php that is used (included) on each page of the site. (And I am not a programmer :) )
Help is always appreciated - thanks!
<?php
$path = ("/portfolio");
$size = ("WIDTH=525 HEIGHT=41 BORDER=0");
$self = $_SERVER['PHP_SELF'];
if (strstr($PHP_SELF,"$path/about.php")) {echo "<h1>About Page Header HTML/CSS Here!</h1>";}
elseif (strstr($PHP_SELF,"$path/index.php")) {echo "Home Page Header Text Here";}
elseif (strstr($PHP_SELF,"$path/design/index.php")) print ("<IMG SRC=$path/images/header_design.jpg $size>");
elseif (strstr($PHP_SELF,"$path/articles/index.php")) print ("<IMG SRC=$path/images/header_articles.jpg $size>");
else {echo "<h1>Hello World.</h1>";}
?>
Note : $PHP_SELF in (strstr($PHP_SELF,"$path/about.php"))
Shouldn't it be $self, e.g. (strstr($self,"$path/about.php"))
<?php
$path = ("/portfolio");
$size = ("WIDTH=525 HEIGHT=41 BORDER=0");
$self = $_SERVER['PHP_SELF'];
if (strstr($self ,"$path/about.php")) {echo "<h1>About Page Header HTML/CSS Here!</h1>";}
elseif (strstr($self ,"$path/index.php")) {echo "Home Page Header Text Here";}
elseif (strstr($self ,"$path/design/index.php")) print ("<IMG SRC=$path/images/header_design.jpg $size>");
elseif (strstr($self ,"$path/articles/index.php")) print ("<IMG SRC=$path/images/header_articles.jpg $size>");
else {echo "<h1>Hello World.</h1>";}
?>

WordPress/php if statement within an if statement

I'm having one of those moments where I know I'm so close, and probably missing something super minor that's causing this to not work.
I'm using a pre-built theme, with an if statement for including or not including a link. What I want to do it put an if statement inside of THAT differentiating between internal and external links, opening external links in a new tab.
This is the portion I'm specifically working on
if(!empty($parallax_one_service_box->title)){
if( !empty($parallax_one_service_box->link) ){
if($parallax_one_service_box->link_type == 'External'){
echo '<h3 class="colored-text">'.esc_attr($parallax_one_service_box->title).'</h3>';
}else {
echo '<h3 class="colored-text">'.esc_attr($parallax_one_service_box->title).'</h3>';
}
}
else {
if (function_exists ( 'icl_translate' ) && !empty($parallax_one_service_box->id)){
echo '<h3 class="colored-text">'.icl_translate('Featured Area',$parallax_one_service_box->id.'_services_title',esc_attr($parallax_one_service_box->title)).'</h3>';
} else {
echo '<h3 class="colored-text">'.esc_attr($parallax_one_service_box->title).'</h3>';
}
}
}
So it's something along the lines of "if not empty, if external link open in a new tab." I ran it through php code checker, and the code is correct...not missing any brackets or anything. The problem seems to be that it's either opening all in a new tab or opening none in a new tab...it's not differentiating between external and internal. So I'm guessing something is wrong with the "if external" line...
Here's a link to Pastebin with the whole section of code: http://pastebin.com/kmuGiVJv

disable a href links using PHP

I have a page with a menu on for logged in users
i am including this page on all the other pages in my site but i don't want users that are NOT logged in to be able to click the links
How can I disable all the links on that page if a PHP variable = 'no'
i know i can use
if($php_var == 'no') {
//do something here
}
but I'm not sure how to disable the links?
Is there any way using CSS or Javascript to disable links?
try this
if($php_var == "no")
{
echo 'Your Text For Link';
}
else
{
echo 'Your Text For Link';
}
user javascript:void(0); for no redirection. this will maintain your css for link like others but when you click it won't redirect.
If i understood everything correctly, the answer is quite simple. Why dont you just replace the links with plain strings?
if($php_var === "no") {
echo "This is the text of your link.";
} else
{
echo "This is the text of your link.";
}
But as already mentioned, completely hiding the links is better, as usual users gets confused by such things.
this will remove all href from a tags. If php var is no. Put this code after all a tags else won't work
<?php
if($php_var === "no"){
echo '<script>var x=document.getElementsByTagName("a");for (i=0;i<x.length;i++){x[i].removeAttribute("href");}</script>';
}
?>
You would need to do the processing pre-output, PHP will not dynamically disable the href of an already created DOM element.
If you are producing the output of the links via PHP, you could do something like:
echo 'Link';
Otherwise, you could create an AJAX call to the PHP script, and if it returns 'no', iterate through your pages links and disable the links via JavaScript.
<a href='<?php echo ($php_var == "no") ? "javascript:void(0)" : "link.php" ?>'>
Hello user
</a>
you could do this:
// define if you want to make links work
$linking = true;
Then your link:
<a <?php if($linking == true) { ?> href="..." <?php } ?>>Link</a>
If links are not shown, I'd also add some CSS:
.link_that_is_no_link {
text-decoration: none;
cursor: default
}
How do you check if the user is logged in or not? Do you use sessions? The same way you check for the user if he is logged in you can decide to show items or not.
You can do both:
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
echo 'LINK';
}
that will keep showing the link but will lead nowhere if the user is not logged in.
Or you can do :
if(isset($_SESSION['id'])){
echo 'LINK';
}else{
//do nothing here or put a link to the login page
}
that will show the link only if you are logged in.
I prefer the second option since I think that no users will like to see a link without being able to open it.
Note that code in this answer is just a guess of your real code
You can use PHP if-else condition and write HTML like this:
<a href="" onclick="return false;">

PHP include inside of an existing code?

Here's my code:
<?php
if(isset($_GET['p']))
{
$nshortname = strip_tags($_GET['p']);
$check = mysql_query("SELECT * FROM pages WHERE `shortname` = '$nshortname'");
if(mysql_num_rows($check) == 0)
{
echo '<center><font size="50" style="font-weight:bold;">404</font><br>Appears this page is a dead end</center>';
}
else
{
$h = mysql_fetch_array($check);
//post details
$title = $h["title"];
$content = $h["content"];
$shortname = $h["shortname"];
// Start of page content
echo '
<p>
<font size="5pt">'.$title.'</font><br><hr>
'.$content.'<br>
';
// End of page content
}
}
else
{
echo 'No page has been selected to view';
}
?>
What it does exactly, is it grabs pages from my database and reads them, so for example if I have a page in that table called "test" I can go to it by http://mylink.com/?p=test. Although i've come up with an issue. On one of those pages that come from the database I want to include but when I type it into the database field and go back to the page it shows with nothing.
I went to the source of the page in my browser and found out the code turned into <!--?php include "inc/extra/plugins/header/slideshow.php"?-->
Does anyone know how I can sold it from turning into <!--? and make my include code work.
I would caution against using eval() of unknown content. Basically, the content comes from your database, but that doesn't guarantee it's safe to execute as code! There are a lot of ways it could cause errors or do something malicious.
But you also have other dangerous security gaffes in your code. You should learn about how to defend against SQL injection vulnerabilities and Cross-Site Scripting (XSS) vulnerabilities and File Inclusion vulnerabilities.
Use mysql_real_escape_string() if you are still using the deprecated ext/mysql. But if you can, switch to mysqli or PDO_mysql and use prepared statements with parameters.
Always output dynamic content with htmlspecialchars(). What if the content contains Javascript code? It could cause mischief.
Never eval() arbitrary content as code. You have no control over what that content is, or what it could do when you execute it.
Be as restrictive as possible - if you want to include a file, store the filename separately from content (e.g. in a separate column), and use it only for including files.
Here's an example with some of these problems fixed in your code:
<?php
if(isset($_GET['p']))
{
$nshortname = mysql_real_escape_string($_GET['p']);
$check = mysql_query("SELECT * FROM pages WHERE `shortname` = '$nshortname'");
if(mysql_num_rows($check) == 0)
{
echo '<center><font size="50" style="font-weight:bold;">404</font><br>Appears this page is a dead end</center>';
}
else
{
$h = mysql_fetch_array($check);
//post details
$title = htmlspecialchars($h["title"]);
$content = htmlspecialchars($h["content"]);
$shortname = $h["shortname"];
// Start of page content
echo '
<p>
<font size="5pt">'.$title.'</font><br><hr>
'.$content.'<br>
';
// End of page content
// Start of include
if ($h["include"]) {
// strip out anything like "../../.." etc.
// to make sure this is only a simple filename.
$include = basename($h["include"]);
include "inc/extra/plugins/header/{$include}.php";
}
// End of plugin inclusion
}
}
else
{
echo 'No page has been selected to view';
}
?>
Also check out http://www.sitepoint.com/php-security-blunders/ and http://phpsec.org/projects/phpsecinfo/
Re your comments:
To allow a limited set of basic HTML, the best tool you need to use is http://htmlpurifier.org
I'm not sure what to say about your include displaying code instead of working. I just tested this, and the following two files seem to work exactly as intended:
foo.php:
<?php
echo "<h1>START FOO</h2>";
if ($_GET["include"]) {
$include = basename($_GET["include"]);
include "./{$include}.php";
}
echo "<h1>END FOO</h2>";
bar.php:
<?php
echo "<h2>BAR</h2>";
If you have a variable $content which is html with php, you can use
eval("?>" . $content . "<?php");
This will output $content having processed all the <?php ?> tags.

PHP Check if page contains

I'm looking for a quick code/function that will detect if a page contains a certain thing.
It's for a new project I'm working on.
Basically, the user will paste a simple javascript code into their pages, but I need to make sure they do.
I need a code that will scan through a specific webpage url and find the code I provided.
Thanks!
You want to scan through a webpage, not an URL! You get to the webpage through an URL. :)
<?php
$contents = file_get_contents("http://some.site/page.html");
$search = <<<EOF
<script type="text/javascript">
alert('They must have this!');
</script>
EOF;
if (strpos($contents, $search) === FALSE) {
echo "Naughty webpage!";
}
?>
Note, though, that programmatically skimming pages like this is generally considered bad form.
You can get the contents of a URL as a string, and search the contents for that code:
<?php
function check_url($url) {
$page = file_get_contents($url);
$code = '<script src="http://example.com/test.js"></script>';
if (strpos($page, $code) === FALSE) {
return false;
} else {
return true;
}
}
?>
You may want to swap that simple strpos out for a regular expression, but this will do the trick.
You need to do the 2 things:
1) get the content of remote url
2) check if the content contains your string:
if ( stristr($content, 'your_desired_string') )
{
echo ' Yes, found';
}
there are great libraries for crawling websites like cURL but in your case it seems to be an overkill to use it. If you want to use the cURL library I recommend the Snoopy-class to you which is very simple to use.
Felix

Categories