Link to home
Start Free TrialLog in
Avatar of pwlovesrs
pwlovesrsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How make login in diff page?

I has just wish this people would not see "Login" on news... i got some idea i would like login moved in new page like login.html or something.... anyone help me took the code off and paste on diff page
Avatar of pwlovesrs
pwlovesrs
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER


<?php
/*
phpFastNews 1.0.0
Last update: 02 Sep 2008

Skipping the blah-blah-blah I kindly ask you to keep the link back to phpFastNews website. Thanks!
*/

include_once( dirname(__FILE__) . '/fastnews-conf.php' );
define( 'DATAFILE', dirname(__FILE__) . '/fastnews-data.txt' );

$JAVASCRIPT = <<<EOT
<SCRIPT LANGUAGE="JavaScript">
function fn_HideElement( elementName ){
	ele = document.getElementById( elementName );
	if( ! ele ){
		alert( 'cannot find ' + elementName );
		return;
		}
	ele.style.display = "none";
	}
function fn_ShowElement( elementName ){
	ele = document.getElementById( elementName );
	if( ! ele ){
		alert( 'cannot find ' + elementName );
		return;
		}
	ele.style.display = "block";
	}
</SCRIPT>
EOT;

/* PROCESS ACTION */
$action = ( isset($_REQUEST['fn_action']) ) ? $_REQUEST['fn_action'] : '';
if( $action ){
	$fn = new fastNews;
	// NO ACTION IF NOT LOGGED IN
	if( $fn->isLoggedIn() || ($action == 'login') ){
		switch( $action ){
			case 'login':
				$password = $_POST['password'];
				if( $fn->checkPassword($password) )
					$fn->doLogin();
				else {
					$fn->displayError( 'Incorrect Password!' );
					exit;
					}
				break;
			case 'logout':
				$fn->doLogout();
				break;
			case 'add':
				$newItem = $fn->grabData();
				$fn->addItem( $newItem );
				$fn->save();
				break;
			case 'update':
				$newItem = $fn->grabData();
				$itemId = $newItem['id'];
				$fn->updateItem( $itemId, $newItem );
				$fn->save();
				break;
			case 'delete':
				$itemId = $_REQUEST['id'];
				$fn->deleteItem( $itemId );
				$fn->save();
				break;
			}
		}

	/* REDIRECT BACK TO THE REFERRER */
	$referrer = $_SERVER['HTTP_REFERER'];
	header( "Location: $referrer" );
	exit;
	}

class fastNews {
	var $news = array();

	function fastNews(){
		$this->news = array();
		$this->load();
		}

	function checkPassword( $pass ){
		if( $pass == FN_PASSWORD )
			return true;
		else
			return false;
		}

	function doLogin(){
		setcookie( FN_COOKIE_NAME, '1', time() + FN_COOKIE_EXPIRES );
		}

	function doLogout(){
		setcookie( FN_COOKIE_NAME, '', time() -1 );
		}

	function isLoggedIn(){
		if( isset($_COOKIE[FN_COOKIE_NAME]) && $_COOKIE[FN_COOKIE_NAME] )
			return true;
		else
			return false;
		}

	function grabData(){
		$subject = $_POST['subject'];
		$message = $_POST['message'];
		if( get_magic_quotes_gpc() ){
			$subject = stripslashes( $subject );
			$message = stripslashes( $message );
			}
		$itemId = isset($_POST['id']) ? $_POST['id'] : 0;

		$data = array(
			'subject'	=> $subject,
			'message'	=> $message,
			'id'		=> $itemId,
			);

		return $data;
		}
		
	function addItem( $item ){
//		$date = date( "j M Y H:i", time() );
		$date = date( "j M Y g:i a", time() );

		$item['date'] = $date;
		$this->news[] = $item;
		}

	function updateItem( $id, $newItem ){
		$item = $this->news[$id - 1];
		$item = array_merge( $item, $newItem );
		$this->news[ $id - 1 ] = $item;
		}

	function deleteItem( $id ){
		array_splice( $this->news, $id - 1, 1 );
		}

	function save(){
		// CHECK IF THE DATA FILE EXISTS
		if( ! file_exists(DATAFILE) ){
			$this->error( 'Datafile missing! Please create a new empty file <B>' . DATAFILE . '</B> and make chmod 666' );
			return;
			}
		// CHECK IF THE DATA FILE IS WRITABLE
		if( ! is_writable(DATAFILE) ){
			$this->error( 'Datafile is not writable! Please make chmod 666 for the <B>' . DATAFILE . '</B> file.' );
			return;
			}

		$this->setToFile( DATAFILE );
		}

	function load(){
		// CHECK IF THE DATA FILE EXISTS
		if( ! file_exists(DATAFILE) ){
			$this->error( 'Datafile missing! Please create a new empty file <B>' . DATAFILE . '</B> and make chmod 666' );
			return;
			}

		// LOAD THE DATA FILE
		$this->getFromFile( DATAFILE );
		}

	function display(){
		global $NEWS_ITEM, $NEWS_LIST, $JAVASCRIPT;
		$CODE_URL = FN_CODE_URL;

		$view = '';
		$items = '';
		$itemId = count($this->news) + 1;

		// ORDER IN REVERSE
		reset( $this->news );
		foreach( array_reverse($this->news) as $n ){
			$itemId--;
			if( $this->isLoggedIn() ){
				$editLinks = <<<EOT
<A HREF="#" ONCLICK="fn_ShowElement('fn-edit-$itemId'); fn_HideElement('fn-view-$itemId'); return false;">Edit</A>
<A HREF="$CODE_URL?fn_action=delete&id=$itemId" ONCLICK="return confirm('Are you sure to delete this news item?')">Delete</A>
EOT;
				}
			else {
				$editLinks = '';
				}

			$formSubject = htmlentities( $n['subject'] );
			$replaces = array(
				'{SUBJECT}'			=> $n['subject'],
				'{FORM_SUBJECT}'	=> $formSubject,
				'{DATE}'			=> $n['date'],
				'{MESSAGE}'			=> $n['message'],
				'{ITEM_ID}'			=> $itemId,
				'{EDIT_LINKS}'		=> $editLinks,
				'{CODE_URL}'		=> FN_CODE_URL,
				);
			$itemDisplay = str_replace( array_keys($replaces), array_values($replaces), $NEWS_ITEM );
			$items .= $itemDisplay;
			}

		if( $this->isLoggedIn() ){
			$addLink = <<<EOT
<A ID="fn-addBar" HREF="#" ONCLICK="fn_ShowElement('fn-addForm'); fn_HideElement('fn-addBar'); return false;">Add News Item</A>
EOT;
			}
		else {
			$addLink = '';
			}

		if( $this->isLoggedIn() ){
			$loginLink = <<<EOT
<A HREF="$CODE_URL?fn_action=logout">Logout</A>
EOT;
			}
		else {
			$loginLink = <<<EOT
<A ID="fn-loginBar" HREF="#" ONCLICK="fn_ShowElement('fn-loginForm'); fn_HideElement('fn-loginBar'); return false;">Login</A>
EOT;
			}

		// ADD UP OUR CREDENTIALS
		$copyright = <<<EOT
<DIV STYLE="font-size: 0.75em; margin: 1em 0 0 0;">Powered by <A HREF="http://www.phpfastnews.com">phpFastNews</A> - fast and free news display script</DIV>
EOT;
		$items = $items . "\n" . $copyright;

		$replaces = array(
			'{NEWS_ITEMS}'	=> $items,
			'{ADD_LINK}'	=> $addLink,
			'{LOGIN_LINK}'	=> $loginLink,
			'{CODE_URL}'	=> FN_CODE_URL,
			);
		$view = str_replace( array_keys($replaces), array_values($replaces), $NEWS_LIST );

		$view = $JAVASCRIPT . "\n" . $view;

		return $view;
		}

	function setToFile( $fileName ){
		reset( $this->news );
		$content = '';
		foreach( $this->news as $n ){
			if( ! $n )
				continue;
			$content .= 'ITEM_START' . "\n";
			$content .= $n['date'] . "\n";
			$content .= $n['subject'] . "\n";
			$content .= $n['message'] . "\n";
			$content .= 'ITEM_END' . "\n";
			}
		setFileContent( DATAFILE, $content );
		}

	function getFromFile( $fileName ){
		$fileContentArray = file( $fileName );
		reset( $fileContentArray );
		$lineIndex = 0;
		foreach( $fileContentArray as $line ){
			$line = trim( $line );
			if( ! $line )
				continue;

			if( $line == 'ITEM_END' ){
				$this->news[] = $item;
				continue;
				}

			if( $line == 'ITEM_START' ){
				$item = array();
				$lineIndex = 1;
				}
			else {
				// PARSE LINE
				switch( $lineIndex ){
					case 1:
						$key = 'date';
						break;
					case 2:
						$key = 'subject';
						break;
					default:
						$key = 'message';
						break;
					}

				if( isset($item[$key]) )
					$item[$key] .= $line;
				else
					$item[$key] = $line;
				
				$lineIndex++;
				}
			}
		}
		
	function displayError( $msg ){
		echo $msg;
		/* REDIRECT BACK TO THE REFERRER */
		$referrer = $_SERVER['HTTP_REFERER'];
		echo <<<EOT
<meta http-equiv="Refresh" content="1; URL=$referrer">
EOT;
		}

	function error( $msg ){
		echo 'Error: ' . $msg;
		}
	}
?>
<?php
function _print_r( $thing ){
	echo '<PRE>';
	print_r( $thing );
	echo '</PRE>';
	}

function setFileContent( $fileName, $content ){
	$length = strlen( $content );
	$f = fopen( $fileName, 'w' );
	$result = fwrite($f, $content, $length);
	fclose( $f );
	}
?>

Open in new window

on the code, i wanted to move the login bitton to a different page, can anyone help me with this?
i found something wrong on this code is date

check on www.froggieserver.com

so where can i edit the code to make date right??
I can show you how to do a login and a logout.  What does this have to do with the question about the date?  I think that is a separate topic.

Here are my teaching examples of how it is done.  Logout first...
<?php // RAY_logout_example.php
error_reporting(E_ALL);

// MAN PAGE HERE: http://us.php.net/manual/en/book.session.php
// ALWAYS START THE SESSION ON EVERY PAGE
session_start();

// SEE IF THE CLIENT IS ALREADY LOGGED OUT
if (empty($_SESSION["uid"]))
{

// CLIENT IS NOT LOGGED IN
    echo "<br/>YOU ARE NOT LOGGED IN \n";
    echo "<br/>CLICK HERE TO <a href=\"RAY_login_example.php\">LOG IN</a>\n";
    die();
}

// SET THE EXPIRATION FOR COOKIES
define('COOKIE_LIFE', 60*60*24); // A 24-HOUR DAY IN SECONDS ( = 86,400 )
$cookie_expires	= time() - date('Z') - COOKIE_LIFE;

// CLEAR THE INFORMATION FROM THE $_SESSION ARRAY
$_SESSION = array();

// IF THE SESSION IS KEPT IN COOKIE, FORCE SESSION COOKIE TO EXPIRE
if (isset($_COOKIE[session_name()]))
{
   setcookie(session_name(), '', $cookie_expires, '/');
}

// TELL PHP TO ELIMINATE THE SESSION
session_destroy();


// OPTIONAL - CLEAR ALL COOKIES
// foreach ($_COOKIE as $key => $value)
// {
//    setcookie($key, '', $cookie_expires, '/');
// }

// OPTIONAL - REDIRECT TO THE HOME PAGE
// header("Location: /");
// exit;


// GIVE THE CLIENT A LINK TO THE LOGIN
echo "<br/>YOU ARE LOGGED OUT\n";
echo "<br/>CLICK HERE TO <a href=\"RAY_login_example.php\">LOG IN</a>\n";

Open in new window

Here is the login script...
<?php // RAY_login_example.php
error_reporting(E_ALL);

// ALWAYS START THE SESSION ON EVERY PAGE
session_start();

// IF THERE IS uid IN THE COOKIE, PUT IT IN THE SESSION TO SHOW CLIENT IS LOGGED IN
if (!empty($_COOKIE["uid"]))
{
    $_SESSION["uid"] = $_COOKIE["uid"];
}

// IF THERE IS uid DATA IN $_POST, PROCESS IT HERE
if (!empty($_POST['uid']))
{
// FIRST, VOID THE SESSION VALUE
    $_SESSION["uid"] = NULL;

// IS THE UID CORRECT? - IN THE REAL WORLD THIS IS A DATA BASE LOOKUP
    $uid = trim(strtolower($_POST["uid"]));
    if ($uid == 'hello')
    {

// YES, THE UID IS CORRECT - COMPLETE THE LOGIN BY STORING THE UID IN THE SESSION ARRAY
        $_SESSION["uid"] = $uid;
    }
    else
    {

// NO, THE UID IS NOT CORRECT
        echo "<br/>WRONG UID - YOU ARE NOT LOGGED IN";
    }
}



// SEE IF THE CLIENT IS ALREADY LOGGED IN
if (isset($_SESSION["uid"]))
{
// THIS WOULD BE THE RIGHT PLACE TO SET THE COOKIE TO KEEP CLIENT LOGGED IN OVER TIME ACROSS MULTIPLE BROWSER SESSIONS
    /* SET COOKIE HERE */

// CLIENT IS LOGGED IN
    echo "<br/>YOU ARE LOGGED IN AND YOUR UID IS {$_SESSION["uid"]}\n";
    echo "<br/>CLICK HERE TO <a href=\"RAY_logout_example.php\">LOG OUT</a>\n";

// SHOW THE CONTENTS OF THE SUPERGLOBAL ARRAYS
    echo "<pre>\n";
    echo "_POST: ";    var_dump($_POST);
    echo "_COOKIE: ";  var_dump($_COOKIE);
    echo "_SESSION: "; var_dump($_SESSION);
    echo "</pre>\n";
    die();
}



// IF WE GET HERE, THE CLIENT IS NOT LOGGED IN
// DROP INTO HTML TO PUT UP THE LOGIN FORM
?>
<form method="post">
TO SIMULATE A LOGIN TYPE 'Hello' HERE: <input type="text" name="uid" />
<input type="submit" />
</form>

<?php
// SHOW THE CONTENTS OF THE SUPERGLOBAL ARRAYS
echo "<pre>\n";
echo "_POST: ";    var_dump($_POST);
echo "_COOKIE: ";  var_dump($_COOKIE);
echo "_SESSION: "; var_dump($_SESSION);
echo "</pre>\n";

Open in new window

If u take a look on site... u will see latest news something is wrong about date, also i want hiddien the "login" to somewhere in other page... u know what i mean?

www.froggieserver.com
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well thanks... it good infomations.