Jump to content

Recommended Posts

Hi guys, so I tried searching this, but I had some issues being able to find the proper wording and keywords to query. Everything I searched PHP, HTML, Coding Standard, Coding Style, Coding, Style, Standard, etc. I got varying results. With that being said, if anyone knows of a topic that already addresses this, please feel free to link it and I will gladly do the footwork.

 

^ TL;DR --> I searched, but couldn't find a topic for this...

 

Alright, now down to the brass tacks... I am having some trouble building a function in PHP that will display the header. I believe (if I am not mistaken) that I have seen people warning against peppering PHP code with echo <HTML HERE> throughout your code. I can sort of understand why, but I admit I am not completely sure why this is. I am trying to figure out a way to display HTML without doing the following...

<?php
// --------------------------------------------------------------------------
// ADMIN BAR HANDLER
// --------------------------------------------------------------------------

/**
 * First we need to determine if the user
 * is logged in or not. After determining
 * the user's logged in status, we will then
 * choose to display the admin bar or
 * redirect the user to the login page.
 */
function admin_bar()
{
	if ( defined( USER_IS_LOGGED_IN ) )
	{
		echo "<div class='admin_bar'>
					Admin Bar Here
				</div>";
	}
	
	else if ( defined( USER_IS_NOT_LOGGED_IN ) )
	{
		header( "Location: " . LOGIN_PAGE ) ;
	}
}

?>
Link to comment
https://forums.phpfreaks.com/topic/290076-php-html-practice/
Share on other sites

Not the mixture of PHP and HTML itself is bad but the mixture of application logic and graphical presentation. If you put HTML snippets into your code, then you make the code unreadable and the HTML inflexible. Even the tiniest design update requires you to wade through the entire spaghetti code. In the worst case, you even have to make major changes to the application logic just because you want the baked-in HTML to look differently.

 

So, yes, do separate the two aspects. The minimum is to put all application logic on top on all HTML to the bottom.

 

What on earth does an HTTP redirect have to do with some admin bar in the GUI? Why do you check the log-in status in a function which is supposed to do nothing but render an HTML snippet? This makes no sense. Do all your authentication, redirects and whatnot on top of the script and then render the HTML based on the results.

Link to comment
https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1485993
Share on other sites

Perhaps I am missing something but you deal with two cases if USER_IS_LOGGED_IN is defined and if USER_IS_NOT _LOGGED_IN is defined. There is a third case, neither is defined - I would guess either that this is the first page request for a non-logged in user or that an error occurred. When I code for logins, I just check for LOGGED_IN and if not treat the user as not logged in.

Link to comment
https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1485999
Share on other sites

Perhaps I am missing something but you deal with two cases if USER_IS_LOGGED_IN is defined and if USER_IS_NOT _LOGGED_IN is defined. There is a third case, neither is defined - I would guess either that this is the first page request for a non-logged in user or that an error occurred. When I code for logins, I just check for LOGGED_IN and if not treat the user as not logged in.

 

This is not the actual code I have in place right now, although I generally follow the same rule of thumb. This should have just simply been an IF and ELSE code but either way. With that being said, I just get wary when I mix the HTML, I feel like I'm doing something VERY wrong.

Link to comment
https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1486002
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.