Page 1 of 1

[PHP] Is it cool to do this?

Posted: Wed Feb 20, 2013 7:05 pm
by vitinho444
Hey guys, in the revamping my engine im using the following base:

Code: Select all

<?php
	session_start();
	include("config.php"); 	//Include Config and functions
?>

<!DOCTYPE html>
<html>
	<head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <a href="index.php"><img class="logo" src="Images/Sites/Logo.jpg"></a>
    </head>
    
    <body>
    	<?php 
			if(isset($_SESSION["_PLAYER"]))
			{
				include("menu.php"); 	//Include the dropdown menu
				include("gamemenu.php");//Include the game menu
				
				?>
				<div id="content">
                	<h1>Home</h1>
                    <h2>Message of the Day</h2>
                    <?php
						MessageDay();
					?>
                    <h2>News</h2>
                    <?php
						RetrieveNews();
					?>
            	</div>
                <?php
			}
			else
			{
				Redirect("index.php");
			}
		?>
            
    </body>    
</html>
Ok, all nice i think, but is it cool to put the else on the SESSION in the bottom?

Re: [PHP] Is it cool to do this?

Posted: Wed Feb 20, 2013 8:14 pm
by Elvar
Would't that bring a error? due to redirect, when dom is already send to the client?

Re: [PHP] Is it cool to do this?

Posted: Wed Feb 20, 2013 8:48 pm
by vitinho444
What you mean by "dom" ?

I dont think there's an error.. it checks if a session is set, if not just redirects to the index.php where the login is.

Re: [PHP] Is it cool to do this?

Posted: Wed Feb 20, 2013 9:33 pm
by Elvar
vitinho444 wrote:What you mean by "dom" ?

I dont think there's an error.. it checks if a session is set, if not just redirects to the index.php where the login is.
DOM = Document Object Model, aka Markup. Well, in order to do a redirect server site, the header must not have been sent, which i will have, as this is printet before the redirect.

Code: Select all

<!DOCTYPE html>
<html>
   <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <a href="index.php"><img class="logo" src="Images/Sites/Logo.jpg"></a>
    </head>
    
    <body>
Typically this will yield a "Warning: Cannot modify header information" http://stackoverflow.com/questions/8028 ... ent-by-php.

What i am trying to say, is that you should check if the player session is set, before you start writing the DOM.

Re: [PHP] Is it cool to do this?

Posted: Wed Feb 20, 2013 10:03 pm
by vitinho444
So.. i should check for player session before the <head> ?

Re: [PHP] Is it cool to do this?

Posted: Thu Feb 21, 2013 3:02 am
by Jackolantern
If you want to use an HTTP redirect, it must be sent before anything is sent to the browser, including the initial <html> tag.

Re: [PHP] Is it cool to do this?

Posted: Thu Feb 21, 2013 1:08 pm
by vitinho444
wait wait.. so i cant use header(location...) for redirect purposes if im using the code like i have it now ?

that sucks.. meta refresh it is ?

Re: [PHP] Is it cool to do this?

Posted: Thu Feb 21, 2013 1:45 pm
by Chris
You want to rethink the flow of your application. You are also in a spiral of reinventing something that already exists. Trust me I've been there.

What you wat to do is take a look at some open source php frameworks. It will save you a lot of time and help you understand exatcly what it is you are already trying to do.

As for the header function, I think in newer versions of PHP that it doesn't matter anymore where you call it, the headers are now set in a different way than they used to be and aren't formatted until the whole page has been parsed.

Re: [PHP] Is it cool to do this?

Posted: Thu Feb 21, 2013 2:33 pm
by hallsofvallhalla
but remember that your live server and local server may differ. Test it on your live server.

Re: [PHP] Is it cool to do this?

Posted: Thu Feb 21, 2013 3:23 pm
by vitinho444
hallsofvallhalla wrote:but remember that your live server and local server may differ. Test it on your live server.
omg now i get the error.. sh*t

So should i use meta or what?