Jump to content

Merging flat file systems


Jurge

Recommended Posts

I currently have a flat file user registration and login in system. I also have a simple guestbook which i didn't code myself, the guest book currently requires the user to type in there name and e-mail address to make a post. how ever i would like them to have to login and if they aren't logged in then don't display the guestbook.

 

Live version:

B50crew.co.uk

 

Guestbook.php:

<?php
  // find out the domain:
  $domain = $_SERVER['HTTP_HOST'];
  // find out the path to the current file:
  $path = $_SERVER['SCRIPT_NAME'];
$url = "http://" . $domain . $path  ;
extract($_POST);
if($Submit){
$date = date('D d Y h:i');

$comment=eregi_replace("\r\n","*",$comment);
$comment=eregi_replace("\n","*",$comment);
$fp = fopen("bin/guestbook.nfo","a+");

if(!$fp) {
    echo 'Error: Cannot open file.';
    exit;
}

fwrite($fp, $date."||".$name."||".$email."||".$comment."\n");

fclose($fp);

}
?>



<div align="center">
  <h2><span class="style1">Guestbook </span><br>
  </h2>
</div>
<form name="form1" method="post" action="<?php echo $url;?>">
  <label>Name
  <input type="text" name="name">
  </label>




  <label>Email
  <input type="text" name="email">
  </label>
  <p> </p>
  <p>
    comment<br>
    <textarea name="comment" cols="40" rows="10"></textarea>

    <br>
    <label>
    <input type="submit" name="Submit" value="Sign">
    </label>
    <br>
    </p>
</form>
<hr>

<?php

$userinfo = file("cgi-bin/guestbook.nfo");

   echo '<table border="0">';

foreach($userinfo as $val)

{
   //explode that data into a new array:
   $data = explode("||", $val);


  echo '<tr><td>'.$data[0].'</td></tr>';
   echo '<tr><td>'.$data[1].' Wrote:</td></tr>';

    $data[3]=eregi_replace("\*","<br>",$data[3]);

   echo '<tr><td>'.$data[3].'</td></tr>';
   echo '<tr><td>Email:'.$data[2].'<hr></td></tr>';
}


   echo '</table>';



?>

 

Login:

<?php
  // find out the domain:
  $domain = $_SERVER['HTTP_HOST'];
  // find out the path to the current file:
  $path = $_SERVER['SCRIPT_NAME'];
$url = "http://" . $domain . $path  ;
extract($_POST);
if($Submit){
$date = date('D d Y h:i');

$comment=eregi_replace("\r\n","*",$comment);
$comment=eregi_replace("\n","*",$comment);
$fp = fopen("bin/guestbook.nfo","a+");

if(!$fp) {
    echo 'Error: Cannot open file.';
    exit;
}

fwrite($fp, $date."||".$name."||".$email."||".$comment."\n");

fclose($fp);

}
?>



<div align="center">
  <h2><span class="style1">Guestbook </span><br>
  </h2>
</div>
<form name="form1" method="post" action="<?php echo $url;?>">
  <label>Name
  <input type="text" name="name">
  </label>




  <label>Email
  <input type="text" name="email">
  </label>
  <p> </p>
  <p>
    comment<br>
    <textarea name="comment" cols="40" rows="10"></textarea>

    <br>
    <label>
    <input type="submit" name="Submit" value="Sign">
    </label>
    <br>
    </p>
</form>
<hr>

<?php

$userinfo = file("cgi-bin/guestbook.nfo");

   echo '<table border="0">';

foreach($userinfo as $val)

{
   //explode that data into a new array:
   $data = explode("||", $val);


  echo '<tr><td>'.$data[0].'</td></tr>';
   echo '<tr><td>'.$data[1].' Wrote:</td></tr>';

    $data[3]=eregi_replace("\*","<br>",$data[3]);

   echo '<tr><td>'.$data[3].'</td></tr>';
   echo '<tr><td>Email:'.$data[2].'<hr></td></tr>';
}


   echo '</table>';



?>

 

Session.php

 

<?php

class user {

    var $file_dir = "";

   

    function start($time = 3600) {

        session_set_cookie_params($time , '');

        session_name('afroxav-login');

        session_start();

 

        // Reset the expiration time upon page load

        if (isset($_COOKIE['afroxav-login'])) {

            setcookie('afroxav-login', $_COOKIE['afroxav-login'], time() + $time, '', 'localhost', 0, 1);

        }

 

        if (!isset($_SESSION['info'])) {

            $this->data = array('name' => 'Anonymous', 'logged' => false);

        } else {

            $this->data = $_SESSION['info'];

        }

    }

 

    function check_login() {

        if ($this->data['logged'] !== true) {

            return false;

        } else if ($this->data['logged'] === true) {

            if ($this->data['name'] !== 'Anonymous') {

                return true;

            }

            return false;

        }

        return false;

    }

 

    function login($user, $pass) {

        $logins_raw = @file_get_contents($this->file_dir . 'users.php');

        $logins_processed = str_replace('<?php exit; ?>', '', $logins_raw);

        $logins_array = explode('\n', $logins_processed);

        foreach ($logins_array as $id => $line) {

            $logins[$id] = explode('|', $line);

        }

        $pass = $this->hash_pass($pass);

 

        foreach ($logins as $user_info) {

            if ($user_info[1] == $user) {

                if ($user_info[2] == $pass) {

                    $this->update_session($user_info);

                    return true;

                }

            }

        }

 

        return false;

    }

 

    function hash_pass($string) {

        return hash('sha512', $string);

    }

 

    function logout() {

        $_SESSION['info'] = array('name' => 'Anonymous', 'logged' => false);

        $this->data = $_SESSION['info'];

        return true;

    }

 

    function prep_reg_array($name, $pass, $email, $mod = 'false', $admin = 'false') {

        $id_raw = @file_get_contents($this->file_dir . 'id.php');

        $id = str_replace('<?php exit; ?>', '', $id_raw);

        $id = $id + 1;

        @file_put_contents($this->file_dir . 'id.php', '<?php exit; ?>' . $id);

        return array($id, $name, $this->hash_pass($pass), $email, $mod, $admin);

    }

 

    function register($userdata) {

        $write = file_put_contents($this->file_dir . 'users.php', '\n' . implode('|', $userdata), FILE_APPEND);

        return ($write !== false) ? true : false;

    }

 

    function update_session($array) {

        $_SESSION['info'] = array(

            'id'        => $array[0],

            'name'        => $array[1],

            'pass'        => $array[2],

            'email'        => $array[3],

            'mod'        => $array[4],

            'admin'        => $array[5],

            'logged'    => true

        );

        $this->data = $_SESSION['info'];

    }

};

 

//html related functions

//not related at all with the sessions

function html_start($title) {

    header('Content-type: text/html');

 

    echo "<html>\n";

    echo "<head>\n";

    echo "<title>\n";

    echo $title;

    echo "</title>\n";

    echo "</head>\n";

    echo "<body>\n";

    echo "<h1>\n";

    echo $title;

    echo "</h1>\n";

}

 

function html_nav() {

    global $user;

   

    echo "<div>\n";

    echo "Navigation\n";

    echo "<ul>\n";

    echo "<li><a href=\"?page=home\">Home</a></li>\n";

    if ($user->check_login() == true) {

        echo "<li><a href=\"?page=logout\">Log Out</a></li>\n";

    } else {

        echo "<li><a href=\"?page=login\">Log In</a></li>\n";

        echo "<li><a href=\"?page=register\">Register</a></li>\n";

    }

    echo "</ul>\n";

    echo "</div>\n";   

}

 

function html_end() {

    echo "</body>\n";

    echo "</html>\n";

}

?>

 

 

 

Link to comment
Share on other sites

A simplistic approach would be to use a 'logged_in' session variable / function. By checking if the user os logged in you can 'hide/display' as you chose.  ie...

 

if logged_in is true

  display guest book

else

  do something else

 

 

you can use that method for pages/menus/content/etc

 

EXPERIMENT with small scripts to get the hang of it

 

 

Link to comment
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.