Jump to content

Authenticate and Redirect using Object Oriented PhP - HOW????


dhahlen

Recommended Posts

To create a logout page, all you'd need to do is create a page that has this code:

 

<?php 
sesssion_destroy(); //look this up in the PHP.net manual if you want to know more
// then use the header function to redirect the user
?>

 

then just create a link to that page, and the user will be logged out when they click on it.

 

To check if the user is logged in when they access the login.php page, it would just require a minor tweak:

 

<?php
session_start();
include_once("login_class.php");
$login = new Login();

// added if statement below
if (isset($_SESSION['loggedin']))
{
// redirect user using header() or whatever you want to do here
}

if (isset($_POST['submit'])) // this checks if the submit button has been clicked
{
       // if so, process the form
       $login->processForm();
}
else
{
       // if the button hasn't been clicked, show the form
       $login->generateForm();
}

?>

Link to comment
Share on other sites

added this

 

logout.php

 

<?php
session_destroy();
// then use the header function to redirect the user
header("location: module8.php");
?>

 

When I was clicking on the login page link (form.php) it would keep cycling through the logged in page and the form page (some type of loop, every click would be one or the other, alternating)

 

I moved the logout page to move me to module8.php, and I included this code above form.php

 

<?php
session_start();
include_once("login_class.php");
$login = new Login();
if (isset($_SESSION['loggedin']))
{
       header("location: loggedin.php");
}
?>

 

Now, it seems like the session is not getting destroyed....

Link to comment
Share on other sites

logout.php

 

<?php
session_destroy();
// then use the header function to redirect the user
header("location: module8.php");
?>

 

form.php

 

<html>
<?php // added
include_once("login_class.php");  // added
if (isset($_SESSION['loggedin'])) // added
{
       header("location: loggedin.php"); // added
}
?>
<head>


<style type="text/css">

/* Style Code referenced from http://www.webreference.com/programming/css_frames/index.html */

/* Below are the CSS styles which are referenced throughout the webpage */

body {
 margin:0;
 border:0;
 pading:0;
 height:100%;
 background:#eee;
 font-family:arial, verdana, sans-serif;
 font-size:76%;
 overflow: hidden;
 }

#header {
 position:absolute;
 top:0;
 left:0;
 width:100%;
 height:100px;
 overflow:auto;
 text-align:center;
 background:#53829d;
 color:#fff;
 }

#footer {
 position:absolute;
 bottom:0;
 left:0;
 width:100%;
 height:50px;
 overflow:auto;
 text-align:center; /* Aligns the footer text to the center */
 background:#73a2bd;
 }

#contents {
 position:fixed;
 top:100px; /* This allows the contents of the body to miss the header position */
 left:0;
 bottom:50px; /* This allows the contents of the body to miss the footer position */
 right:0;
 overflow:auto; /* Adds scroll bars if needed */
 background:#fff;
 }

/* navlist referenced from http://css.maxdesign.com.au/listamatic/horizontal01.htm */

#navlist li
 {
 display: inline;
 list-style-type: none;
 padding-right: 20px;
 }

/* Defines the width of the paragraphs */

 p {width:500px;}


/* for internet explorer */

* html body {
 padding:120px 0 50px 0;
 }

* html #contents {
 height:100%;
 width:100%;
 }

</style>

</head>

<body>

<div id="header">
<?php echo "<h1>Welcome to Darren's Website</h1>";?>
<?php echo "<h2>This line uses h2 style with php</h2>";?>
</div>

<div id="footer">
<h3>Test Footer using h3, aligned center</h3>
</div>

<div id="contents">
<div id="navcontainer">
<ul id="navlist">
	<li id="active"><a href="http://cis166.estrellamountain.edu/DARUP97011/module8.php" id="current">Home</a></li>
	<li><a href="guestbook.php">Guest Book</a></li>
	<li><a href="guestbookwrites.php">Guest Book Writes</a></li>
	<li><a href="mailto:DARUP97011@maricopa.edu">Contact Me</a></li>
	<li><a href="contents.txt">Page Code</a></li>
	<li><a href="mailform.php">E-mail Form</a></li>
	<li><a href="login.php">Login Page</a></li>
</ul>
</div>

<h1>Contents of the Body goes here, using h1 style</h1>
<p>
Today's Date is <?php echo date("m-d-Y");?>
<p>
       <h2>Login Example</h2>
       <form action="" method="post">
       <!-- leaving the action attribute blank will cause the page to redirect to itself when the submit button is clicked -->
               <div>
                       <label for="username">Username:</label>
                       <input name="username" id="username" type="text">
                       <br/>
                       <label for="password">Password:</label>
                       <input name="password" id="password" type="text">
                       <br/>
                       <input name="submit" value="Login" type="submit">

               </div>
       </form>

</body>
</html>

 

login.php

 

I tried this with and without the

 

if (isset($_SESSION['loggedin']))
{
       header("location: module8.php");
}

 

it yielded the same results. Here is how it sits on the server

 

<?php
session_start();
include_once("login_class.php");
$login = new Login();

if (isset($_SESSION['loggedin']))
{
       header("location: module8.php");
}


if (isset($_POST['submit'])) // this checks if the submit button has been clicked
{
       // if so, process the form
       $login->processForm();
}
else
{
       // if the button hasn't been clicked, show the form
       $login->generateForm();
}

?>
<p>
</body>
</html>

Link to comment
Share on other sites

Your form.php file does not need this section of code:

 

<?php // added
include_once("login_class.php");  // added
if (isset($_SESSION['loggedin'])) // added
{
       header("location: loggedin.php"); // added
}
?>

 

I'm also confused... your logout script redirects to module8.php and your if statement within login.php that checks if a session variable is set also redirects to module8.php.

 

Shouldn't those be different links? One should link to a login page, and on to the page that the user sees once they have logged in?

Link to comment
Share on other sites

I am now working with Exceptions.

 

The goal here is to throw a catch/try block in 3 different sections.

 

Here is an example provided:

 

//A function that throws an exception, for the example only
function TryThis()
{
    throw new Exception("I tried it, but it didn't work");
}


try
{
  TryThis();
}
catch(Exception $e)
{
  echo "An error occured with the following error<br />" . $e->getMessage();
}

Prints

An error occured with the following error
I tried it, but it didn't work

 

A couple of questions:

 

Question 1: Can you use regex inside of OOP? (lets say I want to validate the format of an name or e-mail address. If it's not in the correct format, then throw an exception, then display a message with the catch) I already have regular expressions in place to validate the e-mail, but I'd like to have it thrown an exception instead. Here is the code I use to validate e-mail:

 

<?php

   	$stringSubject = $_POST['txtSubject'];
   	$stringSubject = str_ireplace("Estrella Mountain","EMCC",$stringSubject);
   	$stringMessage = $_POST['txtMsg'];
	$stringMessage = str_ireplace("Estrella Mountain","EMCC",$stringMessage);

	$validemail = $_POST['txtEmail'];
	$myemailpattern = '^[a-zA-Z0-9\.-_]{1,}@[a-zA-Z0-9\.-_]{1,}(.com|.net|.edu){1}';
if(eregi($myemailpattern,$validemail))
{

   if(isset($_POST['txtEmail']) && isset($_POST['txtSubject']) && isset($_POST['txtMsg']))
   {
       $from = "From: " . $_POST['txtEmail'] . "\r\n";
       $subject = $stringSubject;
       $msg = $stringMessage;

       $result = mail('djwampus@gmail.com',$subject,$msg,$from);

       if($result)
       {
           echo "Your email was successfully sent with the following information<br />";
           echo "Email Address: " . $from . "<br />";
           echo "Subject: " . $subject . "<br />";
           echo "Message: " . $msg . "<br />";
       }
       else
       {
           echo "<h1 style=\"color:red;\">An error occured sending the email!</h1>";
       }
   }
   else
   {
       echo "<h1 style=\"color:red;\">All fields must be filled out!</h1>";
   }
}
else
{
echo "<h1 style=\"color:red;\">Invalid E-mail Address</h1>";
}
?>

 

Question 2: in the example provided below, they use $value >=0 && $value <=23) - is there anyway to change it so it uses characters versus numbers? For example ($value = '^[a-zA-Z0-9\.-_]{1,}')

 

Thanks

Link to comment
Share on other sites

I should preface this by saying I don't use regex/exceptions that much...

 

Question 1: Can you use regex inside of OOP?

Yes.

 

The important parts of your validating email code are

 

$validemail = $_POST['txtEmail']; // input
$myemailpattern = '^[a-zA-Z0-9\.-_]{1,}@[a-zA-Z0-9\.-_]{1,}(.com|.net|.edu){1}'; // pattern
if(eregi($myemailpattern,$validemail))
{
  //if matches...
}

 

Just create a function for the email validation, and then throw exceptions if it doesn't match. My personal opinion would be simply to have the function return true if it matches, false otherwise (and keep any messages that you need to show in one location, rather than split across two functions), but that won't fulfill the requirements for your schoolwork though.

 

is there anyway to change it so it uses characters versus numbers?

I'm not completely sure what you are asking, but like the code I showed above, you could test a regular expression in an if statement.

 

Do keep in mind that the eregi() function is depreciated (http://php.net/manual/en/function.ereg.php). I'd probably suggest using preg_match() instead.

Link to comment
Share on other sites

On the last module!

 

I am just trying to get the NSlookup function to work, it will work if I do not use the $_GET variable. If I just specify a domain (cnn.com) it will work fine. if I try to request data, the page thinks until it times out.

 

<?php
if(isset($_POST['postBack']))
try
{
{
echo '<pre>';
passthru('nslookup' . $_GET["nslookup"]);
echo '</pre>';
}
}
catch(Exception $e)
	{
		echo "<h1 style=\"color:red;\">" . $e->getMessage() . "</h1>";
	}
?>

 

Form data:

 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="nslookup" input type="text" size="10"><br>
<input name="postBack" type="hidden" value="yes">
<input type="submit" value="Perform Lookup">

 

 

The problem I think I am having is this:

 

The passthru command sends the specified line to the server (i.e. not running it in php any longer). Since the actual server has no clue what $_GET is, it simply displays the results of nslookup and does not display the information from the form. That being said, what I need to do is have the text from the form be input into the pass through command before it is executed on the server. Maybe something like this:

 

Type domain in form --> submit --> php takes $_GET["nslookup"] data and then places the data into passthru("nslookup xxxxxx") where xxxxx is the value of $_GET["nslookup"] --> server executes command --> data displayed on page

 

Any ideas?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...