Inserting HTML into PHP without an echo? - php

So on my index page - when it loads I wanted PHP to check if the user was logged in. If the user was logged in it would show the contents of the webpage but if not it would show the login page for the user.
This is the PHP for the check.
<?php
if ($_SESSION['loggedin'] == true) {
echo "contents of the page";
}
else{
echo "the sign up form";
}
?>
My problem now is that right now I have to put all my HTML in an echo statement like this -
echo '<form>inputs</form>';
and that turn into a bunch of yellow text that I can't easily edit because it's being recognized as plain text by the editor(Sublime Text 3)
Maybe there is a way to get the HTML from a different file and link it into the echo?
What do ya'll think? What's the best route to take? Thanks!

You can embed normal HTML after closing PHP tags like this
<?php if(check if user is logged in): ?>
<pure html to show content of page/>
<?php else: ?>
<pure html to show signup form />
<?php endif; ?>
I hope this will help you

You just have to break out of the PHP tag. You can even do it inside a conditional:
if (condition) { ?>
True stuff!
<?php } else { ?>
False stuff...
<?php }

Given the path to your index.html, you could do:
<?php include_once("path/to/index.html"); ?>

You should really consider using php MVC frameworks.
Number one, for this would be Laravel, but you can choose from many others
Symphony, PHALCON, Zend, CodeIgniter.
And regards to IDE I would really recommend PHPStorm, but you can also use others like Eclipse, Netbeans, Aptana Studio....

Related

how to detect last page of pdf in html2pdf with PHP?

I am working on a website which is being developed in PHP zend framework.
I need to create a pdf and i am using html2pdf for that.
Its working alright but i need to put a condition on footer content.
Like if the page is the last page then i need to show some content.
I am using this code to display page numbers.
<i>page [[page_cu]]/[[page_nb]]</i>
But how can i use it in to the condition ?
I have tried like this but m sure its wrong because its giving me an error.
<?php if([[page_cu]]==[[page_nb]] { ?>
Can anyone help me please ?
<?php
if($page_cu == $page_nb)
{
echo "Hello world";
}
?>
"Hello World" will appear only on the last page.
I've used it myself this way.

Cakephp Local session logic

I have a simple if checking for a customer_id in the session.
<?php if($this->Session->read('customer_id')){ ?>
<?php echo $this->element('watchlist'); ?>
<? } else { ?>
<?php echo $this->element('recently_sold'); ?>
<?php } ?>
It's very simple, but when I visit the page, the customer_id is null, and neither of the elements show up. When I login, BOTH the elements are displayed. Something is really strange with the logic here and I'm not sure where to start. I tried checked for null and using is_numeric, but still the same results. Has anyone had any issues with sessions on their local environment like this?
Thanks
Does your PHP set-up recognise the PHP short tags <? ... ?> as opposed to <?php ... ?> ?
If it doesn't then your } else { statement will not be recognized as PHP and so both echo's will happen when the if-statement is true (and you should see the "<? } else { ?>" printed out as text in your source code if you look carefully).
Like #Chris Hendy says above it's confusing to keep opening and closing PHP tags for no reason.
Short tags should not be used as they are a server setting and most servers do not have them on by default. From PHP 5.4 short tags are on a switch with the difference that the "echo short tag "

Hide site content after login

Im creating a site using Adobe Dreamweaver CS6 and I have added a login function there. I have also added a Dynamic Text session MM_Username that displays the logged in users name in the page. However, my problem here is that every time someone is not logged in, dynamic text shows an ugly error message. I'd like to hide this content from my page every time someone logs in. Is there some simple solution to do it like wrapping the text with some php code? Any help would be appreciated since I've pretty much looked everywhere. Thanks in advance.
-John
From memory (been a few years now) I believe in the server behavior panel, there is an option to "show region if" or something similar.
Failing that you can wrap the offending content in an if similar to this:
<?php if (isset($_SESSION['MM_Username'])) { ?> your content here <?php } ?>
Or this which I personally find more readable when mixing html and php:
<?php if (isset($_SESSION['MM_Username'])) : ?> your content here <?php endif; ?>
You can check firstly when user logged_in, If he/she is logged in then,
if(isset(session['var'])){ ?>
< your html content here using CSS hide property >
<?php } else { ?>
< Your html content goes here using CSS show property>
<?php } ?>
The code does exact of what you want for me; hope it helps
<?php if ($_SESSION['MM_UserGroup'] == 'teacher') { ?>
<div class="home"><img src="images/ico-folder.png" width="32" height="31" /></div>
<?php } else { ?>
<?php } ?>

PHP - If URL has this in it, do not show div

I am trying to find the best way to have a DIV be hidden if the url is something. For example I have 2 sites using the same template but only 1 of those sites I want to display something.
So site A is domain.com
Site B is site.domain.com
I want it so if site.domain.com is where people are at then do not show DIV ID="hide". I also need it to have this work for not just that specific URL but for anything that comes after it so site.domain.com/aboutus.php, site.domain.com/contact.php etc....
I would like to do this with PHP or JS.
Just check HTTP_HOST:
<?php
if($_SERVER['HTTP_HOST'] != 'site.domain.com'){
echo '<div>contents</div>'; // display div, he is not on site.domain.com
}
?>
You can probably do something like:
<php if($_SERVER['HTTP_HOST'] == 'example.com'): ?>
<div>This is example.com</div>
<?php else: ?>
<div>This is NOT example.com</div>
<?php endif; ?>
But I think you'd really be better off creating some sort of site1_settings.php that gets included on every page in one site, and then site2_settings.php that gets included in the second. For one thing, this will make it much easier to test your code locally.
I usually use strpos for this.
if(strpos($_SERVER["HTTP_HOST"], "site") !== FALSE)
{
// The user is on site.domain.com
}
else
{
// The user is on domain.com
}
Here would be my javascript implementation. Note: jQuery
if(window.location.indexOf("textinurl"))
{
$('#DivId').hide();
}

php excluding piece of html code?

I have a custom CMS here entitled phpVMS, and I want to exclude a piece of code, a banner for a single page. phpVMS is steered using templates, for instance, the main template that codes the general layout for all pages is entitled layout.tpl. So, like I said, this displays whatever is in the template, on all of the pages. I have however created a special control panel, and therefore require to exclude the banner, because it slightly destroys the theme of it. Is there any PHP code that excludes a piece of code on a single site? I need to remove a single div...
<div id="slideshow"></div>
...on a single page.
Basically, I could create a new template but this is a very long winded and unefficient way within this CMS, and the final result isn't that great - because I can't reinclude the mainbox div which is the box defining the content on the centre white bit of the theme - it's already in the layout.tpl.
I hope you can somehow help me, hope I've included enough information there.
Thanks.
I don't think you can do what you're asking in PHP, but you might be able to do this on the client-side, by either hiding the div (CSS display:none) or by removing it with JavaScript. You might be able to do something like:
<?php
include("layout.tpi");
if (condition)
{
// Javascript:
echo "<script>document.getElementById('slideshow').style.display = 'none';</script>";
// OR jQuery:
echo "<script>$('#slideshow').hide();</script>";
}
?>
If you use a variable to determine you don't want to include the div, you could do this:
<?php if ($include) { ?>
<div id="slideshow"></div>
<?php } ?>
OR
<?php
if (!$include)
echo "<!--";
?>
<div id="slideshow"></div>
<?php
if (!$include)
echo "-->";
?>
EDIT: Obviously, there is no good reason to use the second method. The second method will only comment out the HTML so it will still show up in the source.
I'm not sure if this is what you are looking for, but seems simple
<?
$template = true;
if($template) {
?>
<div id="slideshow"></div>
<?
}
?>
On the template, you could have some code that reads:
if($_SERVER['PHP_SELF'] == /*control panel file*/) {
//exclude
}else{
//include
}

Categories