[PHP] Is it cool to do this?

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

[PHP] Is it cool to do this?

Post 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?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Elvar
Posts: 86
Joined: Sun Oct 07, 2012 7:04 pm

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

Post by Elvar »

Would't that bring a error? due to redirect, when dom is already send to the client?
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

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

Post 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.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Elvar
Posts: 86
Joined: Sun Oct 07, 2012 7:04 pm

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

Post 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.
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

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

Post by vitinho444 »

So.. i should check for player session before the <head> ?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

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

Post 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.
The indelible lord of tl;dr
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

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

Post 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 ?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

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

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

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

Post by hallsofvallhalla »

but remember that your live server and local server may differ. Test it on your live server.
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

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

Post 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?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Advanced Help and Support”