Jump to content

Recommended Posts

Ok, I have been attempting to do a pretty simple task for the last little while, and unfortunately I've failed miserably.

 

I am trying to:

 

Establish the main section of website: index.php

 

I need a registration page: register.php

 

I need a login script that process my login info: login.php

 

I need a script that destroys existinging mess: logout.php

 

register.php (register with forms, no problem inputing into DB), if successful redirect to:

index.php (contains log in forms, enter info, action="login.php")

login.php(process the information, and if successful redirect back to index.php)

With a message displaying you are logged in, giving you the option to log out. (logout is pressed, redirected to logout.php)

logout.php then once destroying redirects once again to index.php with everything cleared displaying you are no longer logged in.

 

I have tried, but I've failed, here are the scripts I've been using, please feel free to edit so they are efficent, and work correctly.

 

Note: database contains: id, username, password, email. The include file is the db connect, so don't worry about any of that.

 

If you have the time to help a poor soul out, I'd much appreciate it. Thanks.

 

index.php

 

<title>rhythmic design</title><?php
session_start();

if (!$_SESSION["valid_user"])
{
Header("Location: login.php");
}



echo "<p>User ID: " . $_SESSION["valid_id"];
echo "<p>Username: " . $_SESSION["valid_user"];
echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);

echo "<p><a href=\"logout.php\">Click here to logout!</a></p>";
?> 

 

register.php

 

<?php


include ("db_connect.inc.php");



if ( $_GET["op"] == "reg" )
{
$bInputFlag = false;
foreach ( $_POST as $field )
	{
	if ($field == "")
   {
   $bInputFlag = false;
   }
	else
   {
   $bInputFlag = true;
   }
	}

if ($bInputFlag == false)
	{
	die( "Problem with your registration info. "
   ."Please go back and try again.");
	}


$q = "INSERT INTO `members` (`username`,`password`,`email`) "
	."VALUES ('".$_POST["username"]."', "
	."PASSWORD('".$_POST["password"]."'), "
	."'".$_POST["email"]."')";

$r = mysql_query($q);


if ( !mysql_insert_id() )
	{
	die("Error: User not added to database.");
	}
else
	{

	Header("Location: index.php");
	}
} 



elseif ( $_GET["op"] == "thanks" )
{
echo "<h2>Thanks for registering!</h2>";
}


else
{
echo "<form action=\"?op=reg\" method=\"POST\">\n";
echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
echo "<input type=\"submit\">\n";
echo "</form>\n";
}

?>

 

login.php

 

<?php
session_start();

include "db_connect.inc.php";

if ($_GET["op"] == "login")
{

if (!$_POST["username"] || !$_POST["password"])
	{
	die("You need to provide a username and password.");
}

$q = "SELECT * FROM `members` "
	."WHERE `username`='".$_POST["username"]."' "
	."AND `password`=PASSWORD('".$_POST["password"]."') "
	."LIMIT 1";


$r = mysql_query($q);

if ( $obj = @mysql_fetch_object($r) )
	{
	$_SESSION["valid_id"] = $obj->id;
	$_SESSION["valid_user"] = $_POST["username"];
	$_SESSION["valid_time"] = time();

	Header("Location: index.php");
	}
else
	{
	 	die("Sorry, could not log you in. Wrong login information.");
	}
}
else
{

echo "<form action=\"?op=login\" method=\"POST\">";
echo "Username: <input name=\"username\" size=\"15\"><br />";
echo "Password: <input type=\"password\" name=\"password\" size=\"8\"><br />";
echo "<input type=\"submit\" value=\"Login\">";
echo "</form>";
}
?>

 

logout.php

 

<?php
session_start();
session_unset();

session_destroy();


Header("Location: index.php");

?>

Link to comment
https://forums.phpfreaks.com/topic/92228-in-need-of-help/
Share on other sites

not functioning properly isn't very clear and no one gonna read through it for you if u don't clarify the issue

 

register.php (register with forms, no problem inputing into DB), if successful redirect to:

index.php (contains log in forms, enter info, action="login.php")

login.php(process the information, and if successful redirect back to index.php)

With a message displaying you are logged in, giving you the option to log out. (logout is pressed, redirected to logout.php)

logout.php then once destroying redirects once again to index.php with everything cleared displaying you are no longer logged in.

 

That's my problem, thats what I'm attempting to do. Sorry if I wasn't clear enough, hard to ask a question when the scripts work, but not the way i want them too  :P

Link to comment
https://forums.phpfreaks.com/topic/92228-in-need-of-help/#findComment-472473
Share on other sites

When I tried to change it to the functionality I wanted I ended up having problems trying to redirect back to the index.php without any errors. I wish I would have saved some of the errors, but basically it did not follow the path I would have liked.

 

I'm just seeing if I need to clarify more things, do you know what I'm trying to accomplish?

Link to comment
https://forums.phpfreaks.com/topic/92228-in-need-of-help/#findComment-472477
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.