Using PHP if else within default.php

Post Reply
slygar
Posts: 2
Joined: Wed Mar 14, 2012 7:31 pm

Using PHP if else within default.php

Post by slygar »

I'm looking for a bit of help. I want to serve a php include page based on the url or page name instead of displayMainContent . For example: If you are on the home page you would see the contents of the php include (that would not be editable), but if you are on the About page, WonderCMS would work a normal.

Thanks in advance.
xss
Support/Developer
Posts: 22
Joined: Thu May 26, 2011 9:07 pm

Re: Using PHP if else within default.php

Post by xss »

Hello slygar,

the quickest way to achieve this would be to add some line into the index.php file as follows. Change (starting at or around line 81):

Code: Select all

   function displayMainContent()
   {
	   global $cookie, $content, $page;
	   
	   if($_COOKIE[$cookie])
	   {
		   echo "<div class='title'><div id='change'><span id='$page' class='editText'>$content[0]</span></div></div>";
	   }
	   else
	   {
		   echo $content[0];
	   }
   }
to this:

Code: Select all

   function displayMainContent()
   {
	   global $cookie, $content, $page;
	   
     if($page == "Home") {
      include_once('inc/home.php');
     }

	   else if($_COOKIE[$cookie])
	   {
		   echo "<div class='title'><div id='change'><span id='$page' class='editText'>$content[0]</span></div></div>";
	   }
	   else
	   {
		   echo $content[0];
	   }
   }
The "Home" refers to the affected page in your navigation, and 'inc/home.php' is the local path to the file to include. If you want to include a file from a remote location, I think you have to play around with your PHP settings, see here on that.

The file to include should only contain the relevant HTML tags. If you include a complete web page this way, it will most likely screw up your HTML validity, meaning it may have unforeseen effects in some browsers.

I'm sure there are other, possibly cleaner ways, but I'm not certain if that is where WonderCMS is aiming to go. :)

Please, let us know if this works for you.

Kind regards,
xss
¿ן ʇ,uop 'spɹɐʍʞɔɐq ןןɐ ʇı ʇoƃ ן 'ɹɐǝp ɥo
slygar
Posts: 2
Joined: Wed Mar 14, 2012 7:31 pm

Re: Using PHP if else within default.php

Post by slygar »

Thanks for the help. I'll test it out tonight. I agree with you that I was going beyond what WonderCMS was made for.

Thanks again.
esometric

Re: Using PHP if else within default.php

Post by esometric »

Hellow after this login is not work
My index.php:

Code: Select all

<?php

function getSlug( $page ) {
   $page = strip_tags( $page );
   preg_match_all( "/([a-z0-9A-Z-_]+)/", $page, $matches );
   $matches = array_map( "ucfirst", $matches[0] );
   $slug = implode( "-", $matches );
   return $slug;
}
   $page = $_REQUEST['page'];
   if(!$page) $page = "Home";

   $contentfile = $page = getSlug( $page );
My default.php:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>



<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<?php

	echo "<title>$title</title>\n\n".

	"<meta name='description' content='$description' />\n".

	"<meta name='keywords' content='$keywords' />\n".

	"<meta name='generator' content='WonderCMS 0.4.1' />\n";

	editTags();

?>



<link rel="stylesheet" type="text/css" href="template/krneky_blue/style.css" media="screen"/>

<!--[if IE 6]>

	<link rel="stylesheet" href="template/krneky_blue/ie6.css" media="screen, projection">

<![endif]-->

</head>

 

<body>



<div class="container">



<h1 class="header"><a href="./"><?php echo "$title";?></a></h1>



<div class="slogan"><?php echo "$slogan";?></div>


<div class="meniu">
<?php

   echo "<ul id='nav'>";

   displayMenu("<li><a ","</a></li>");

   echo "</ul>";

?> 
</div>
<div class="insider">
<?php displayMainContent();?>
</div>
<div class="insider2">
<?php displaySectionContent(1,'top');?>
</div>
<div class="footer">
<?php if($_COOKIE[$cookie]) extraSettings();?>
<?php echo "$lstatus | $copyright | $mess";?>
</div>
<div class="settings">
<?php if($_COOKIE[$cookie]) extraSettings();?>
</div>
</body>
</html>

   $content[0] = @file_get_contents("files/$contentfile.txt");
   $content[1] = @file_get_contents("files/$contentfile-top.txt");
   if(!$content[0]) $content[0] = "Your page named <b>$page</b> is created.<br /><br />\n\nClick here to start editing your newly created page!";
   if(!$content[1]) $content[1] = "Your section is created.<br /><br />\n\nClick here to start editing your newly created section!";

   $title = @file_get_contents('files/title.txt');
   if(!$title) $title = "Your title here";
   $slogan = @file_get_contents('files/slogan.txt');
   if(!$slogan) $slogan = "- Your slogan over here!";

   $menu = @file_get_contents('files/menu.txt');
   if(!$menu) $menu = "Home";
   
   $description = @file_get_contents('files/description.txt');
   if(!$description) $description = "Enter your website description over here!";
   $keywords = @file_get_contents('files/keywords.txt');
   if(!$keywords) $keywords = "Enter, your, keywords, for, your, website, over, here";

   $copyright = @file_get_contents('files/copyright.txt');
   if(!$copyright) $copyright = "Your website (c) 2011";
   $mess = 'Powered by <a href="http://krneky.com/en/wondercms">WonderCMS</a>';

//config section
	$hostname = $_SERVER['PHP_SELF'];
	$hostname = str_replace('index.php', '', $hostname);
	$hostname = str_replace($page, '', $hostname);

   $theme = $_REQUEST['theme'];
   if( !file_exists("$theme.php") ) $theme = "default";

   $cookie      = 'wondercms';	// cookie ame
   $expirytime  = time()+86400; // expire time
		
   if(isset($_REQUEST['logout']))
   {
	   setcookie($cookie,'',time() - 84000); // remove cookie/
	   header('Location: ./');
	   exit;
   }
   $password = @file_get_contents("files/password");
   if(!$password)
   {
	   savePassword("admin");
   }
   if($_COOKIE[$cookie])
   {
	   $lstatus = "<a href='$hostname?logout'>Logout</a>";
   }
   else	$lstatus = "<a href='$hostname?login'>Login</a>";
   
   if(isset($_REQUEST['login']))
   {
	   getLoginForm();
   }
   require("$theme.php");

// Functions
   function editTags()
   {
	   global $cookie;
	   if(!$_COOKIE[$cookie]) return;

	   echo  "<script type='text/javascript' src='./js/editInplace.js'></script>";
   }

   function displayMainContent()
   {
	   global $cookie, $content, $page;
          if($page == "Home") {
          include_once ('m.php');
          }
	   
	   else if($_COOKIE[$cookie])
	   {
		   echo "<div class='title'><div id='change'><span id='$page' class='editText'>$content[0]</span></div></div>";
	   }
	   else
	   {
		   echo $content[0];
	   }
   }

// display section content
   function displaySectionContent($cnum,$slug)
   {
	   global $cookie, $content,$contentfile;
	   
	   if($_COOKIE[$cookie])
	   {
		   echo "<div id='change'><span id='$ontentfile-$slug' class='editText' style='display:block'>$content[$cnum]</span></div>";
	   }
           else	echo $content[$cnum];
   }

   function displayMenu($stags,$etags)
   {
	   global $menu;
	   
	   $mlist = explode("<br />",$menu);
	   $num = count($mlist);

	   for($ix=0;$ix<$num;$ix++)
	   {
		   $page = trim($mlist[$ix]);
		   if(!$page) continue;
		   echo "$stags href='$page'>$page $etags \n";
	   }
   }
   
   function getLoginForm()
   {
	   global $content, $msg;
	   
	   $msg = "";
	   
	   if (isset($_POST['sub'])) loginSubmitted();
	   $content[0] = "
<center>
<form action='' method='POST'>
<h2>Password</h2>
<input type='password' name='password' /><br />
<input type='submit' name='login' value='Login'>
<h2>$msg</h2><br />

<script src='js/editInplace.js'></script> 
<div class='all'>
<a href='javascript:showhide()'>Click to change your password</a> <br /><br />
<div id=hide style='display: none;'>
Type your <b>old</b> password above, and your new one in the field below.
<h2>New Password</h2>
<input type='password' name='new' /><br />
<input type='submit' name='login' value='Change'>
<input type='hidden' name='sub' value='sub'>
</div></div>
</form></center>";
   }
   
   function loginSubmitted()
   {
	   global $cookie, $password, $msg, $expirytime, $submitted_pass, $page;
	   
	   $submitted_pass = md5($_POST['password']);

	   if ($submitted_pass<>$password)
	   {
		   $msg = "<b class='wrong'>Wrong Password</b>";
		   return;
	   }
	   if($_POST['new'])
	   {
		   savePassword($_POST['new']);
		   $msg = "Password changed!<br /><br />Please login again.";
		   return;
	   }
	   setcookie($cookie,$password,$expirytime);
	   header('Location: ./');
	   exit;
   }
   
   function savePassword($password)
   {
	   $password = md5($password);
	   $file = @fopen("files/password", "w");
	   if(!$file)
	   {
		   echo "<h2 style='color:red'>*Error* - unable to access password</h2><h3>But don't panic!</h3>".
				   "Set the correct read/write permissions to the password file. <br />
				    Find the password file in the /files/ directory and CHMOD it to 640.<br /><br />
				    If this doesn't work, use <a href='http://krneky.com/forum'>this forum</a>.";
		   exit;
	   }
	   fwrite($file, $password);
	   fclose($file);
   }

   function extraSettings()
   {
	   global $cookie, $description, $keywords, $title, $slogan, $copyright, $menu;
	   echo "<div class='settings'>
<h3>Extra Settings</h3>
<a href='javascript:showhide()'>Cick here to open/close settings</a> <br /><br />
<div id=hide style='display: none;'>
<ul class='linkss'>
<b>Add Page: (in a new row) and <a href='javascript:location.reload(true);'>click here to refresh the page</a>.</b><br />
<div id='change'><span id='menu' class='editText'>$menu</span></div>
</ul>
<ul class='linkss'>
<b>Title:</b><br />
<div id='change'><span id='title' class='editText'>$title</span></div>
</ul>
<ul class='linkss'>
<b>Slogan:</b><br />
<div id='change'><span id='slogan' class='editText'>$slogan</span></div>
</ul>
<ul class='linkss'> 
<b>Meta Description:</b><br />
<div id='change'><span id='description' class='editText'>$description</span></div>
</ul>
<ul class='linkss'>
<b>Meta Keywords:</b><br />
<div id='change'><span id='keywords' class='editText'>$keywords</span></div>
</ul>
<ul class='linkss'>
<b>Copyright:</b><br />
<div id='change'><span id='copyright' class='editText'>$copyright</span></div>
</ul>
<br />
If you want to use a copyright sign: &copy; - Simply copy this into your footer: &&#99;&#111;&#112;&#121;&#59;
</div></div>";
   }
?>
Last edited by xss on Mon Oct 29, 2012 11:38 pm, edited 2 times in total.
Reason: Fixed [code] tags
xss
Support/Developer
Posts: 22
Joined: Thu May 26, 2011 9:07 pm

Re: Using PHP if else within default.php

Post by xss »

esometric,

it looks like you did not copy-paste the two files in their correct order. Their contents look mixed up here. In case they really do look the way you posted them, then the login is likely the least of your problems. ;) In this case, I would recommend you to remove your WonderCMS instance and re-install it and carefully re-apply the changes you may have done.

Please, edit your post and re-post your question and your files in an orderly manner so we can try to help you. Also, I'm not sure whether your question is even related to this topic. If it is not on this very same issue, please, post your question in a new topic instead.
¿ן ʇ,uop 'spɹɐʍʞɔɐq ןןɐ ʇı ʇoƃ ן 'ɹɐǝp ɥo
Post Reply