Page 1 of 1

Doubt, define veribales on an if cicle

Posted: Sun May 24, 2009 2:10 pm
by Fearful
Hi! well I have the folowing code on the registration page:

Code: Select all

else if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
      echo "<p>Registered!</p>";
      echo "<p>Thank you <b>".$_SESSION['reguname']."</b>, your information has been added to the database, "
          ."you may now <a href=\"http://www.rushed.biz/index.php\">log in</a>.</p>";   
      mkdir("../user/".$_SESSION['reguname'], 0700);
      mkdir("../sketch/".$_SESSION['reguname'], 0700);  
      mkdir("../gallery/".$_SESSION['reguname'], 0700);
      mkdir("../faves/".$_SESSION['reguname'], 0700);
      mkdir("../journal/".$_SESSION['reguname'], 0700);
      $ruta=("../user/".$_SESSION['reguname']);
      $ext="index.php";
      $archivo=$ruta."/".$arch.$ext;
      $copier=("../scripts/user.php");
 
      $fp=fopen($archivo,'a+');
      copy($copier, $archivo);
      $cadena="";
      fwrite($fp,$cadena);
      fclose($fp);
 
}
Creating a new file on the created dir, I want to copy the code of a sample page, into the new one, but I want to define the users variables on that code before closing the if cicle. I don't know If I explain myself, sorry for my creappy english.

See Ya!

Re: Doubt, define veribales on an if cicle

Posted: Sun Jun 07, 2009 11:04 am
by McInfo
Your question is about templates. Here are two easy ways to handle templates. In an attempt to keep things simple, I have omitted some conditional statements.

A:

a-template.php - This is a sample page.

Code: Select all

<p>This is a template</p>
<p><?php echo $var; ?></p>
a-index.php - This script includes the template as if to display the contents, but captures the output in a buffer and saves it to a new file. During the include, PHP parses the template, so "<?php echo $var; ?>" is converted to "Hello". The buffer is cleaned so that all that is displayed is "Done." and a link to the new file.

Code: Select all

<?php
$var = 'Hello';
ob_start();
include 'a-template.php';
file_put_contents('a-new.html', ob_get_contents());
ob_end_clean();
?>
<p>Done.</p>
<p><a href="a-new.html">a-new.html</a></p>
B:

b-template.html - This is a different kind of template. It will not be parsed as PHP.

Code: Select all

<p>This is a template</p>
<p>{var}</p>
b-index.php - This script reads the template file into a string, replaces "{var}" with "Hello", and saves content to a new file. Then it displays "Done." and a link to the new file.

Code: Select all

<?php
$var = 'Hello';
$template = file_get_contents('b-template.html');
$template = str_replace('{var}', $var, $template);
file_put_contents('b-new.html', $template);
?>
<p>Done.</p>
<p><a href="b-new.html">b-new.html</a></p>
Edit: This post was recovered from search engine cache.

Re: Doubt, define veribales on an if cicle

Posted: Sun Jun 07, 2009 12:09 pm
by kalebaustin

Code: Select all

else if(isset($_SESSION['regsuccess'])){
   /* Registration was successful */
   if($_SESSION['regsuccess']){
Double if?