Jump to content

[SOLVED] Session Login


Akenatehm

Recommended Posts

Hey Guys,

 

I am havin trouble with this script:

<?php
if (isset($_COOKIE['username']))
{
$username = $_COOKIE['username'];
$pass = $_COOKIE['pass'];
$pass = ($pass);

$checkcookie = mysql_query("SELECT * FROM accounts WHERE username = '$username'") or die(mysql_error());

	if (!$checkcookie)
	{
		die('MySQL Error' . mysql_error());
	}	

	else
	{
	$filterresults = mysql_fetch_assoc($checkcookie);
	$theirusername = $filtertheirusername['username'];
	$theirpass = $filtertheirusername['pass'];

	if($username != $theirusername | $pass != $theirpass)
	{
		echo "Incorrect Something";

if (isset($_POST['submit']))
{
if($_POST['password'])
{
	$dbhost = "localhost";
	$dbuser = "user";
	$dbpass = "pass";
	$dbname = "game";
	$connect = mysql_connect($dbhost,$dbuser,$dbpass);

	if (!$connect)
	{
		die('MySQL Error ' . mysql_error());
	}

	else
	{
		$selectdb =	mysql_select_db($dbname);
		if (!$selectdb)
		{
			die('MySQL Error ' . mysql_error());
		}		
		else
		{
		$username = $_POST['username'];
		$pass = $_POST['pass'];
		$pass = md5($pass);

		$checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'") or die(mysql_error());

		echo "Check user failed";

			if (!$checkuser)
			{
				die('MySQL Error' . mysql_error());
			}	

			else
			{
			$filterresults = mysql_fetch_assoc($checkuser);
			$theirusername = $filtertheirusername['username'];
			$theirpass = $filtertheirusername['pass'];

			if($username != $theirusername | $pass != $theirpass)
			{
				$error = "Incorrect Username or Password";
			}
			else
			{
			session_start();
			$_SESSION["username"] = $username;
			$_SESSION["pass"] = $pass; 
			echo "session created";
			}
			}
		}
	}
}
}
}
else{
header("Location:gameindex.php");
}
}
}
?>


<html>
<head>
<title>Login - Fall of The Empire</title>
<link rel="stylesheet" href="styles.css" type="text/css"/>
</head>
<body bgcolor="272d31">
	<div align="center" class="header">
		<img src="images/logo.png" width="473" height="227" alt="Logo">
</div>
<br>
<center>
<div class="main">
<div class="navbar">
	<img src="images/nav_top.png" width="232" height="46" alt="Nav Top"><br>
	<a href="index.php"><img src="images/nav_home.png" width="232" height="33" alt="Nav Home" border="0"></a><br>
	<a href="news.php"><img src="images/nav_news.png" width="232" height="32" alt="Nav News" border="0"></a><br>
	<a href="about.php"><img src="images/nav_about.png" width="232" height="23" alt="Nav About" border="0"></a><br>
	<a href="login.php"><img src="images/nav_login.png" width="232" height="32" alt="Nav Login" border="0"></a><br>
	<a href="register.php"><img src="images/nav_register.png" width="232" height="25" alt="Nav Register" border="0"></a><br>
	<a href="contactus.php"><img src="images/nav_bottom.png" width="232" height="179" alt="Nav Bottom" border="0"><br></a>
	</div>	

	<div class="cb">
	<img src="images/login.png" align="right" class="welcome">
	<br>
	<br>
	<br>
	<br>
	<table width="300px" align="right">
		<form action="<?php echo $PHP_SELF; ?>" method="POST">
			<tr>
				<td>
					<font color="white">
						Username:
					</font>
				</td>

				<td>
					<input type="text" name="username">
				</td>
			</tr>

			<tr>
				<td>
					<font color="white">
						Password:
					</font>
				</td>

				<td>
					<input type="password" name="password">
				</td>
			</tr>

			<tr>
				<td>
					<br>
				</td>
				<td>
				</td>
			<tr>
				<td>
				</td>
		<td align="right" style="padding-right:8px">
		<input type="submit" name="submit" value="Login"align="right">
		</td>
		</tr>
		</form>	
	</table>
	<p><?php echo $error; ?></p>
	</div>
	</div>
	</center>
</body>
</html>

 

The script does nothing but refresh the page. I am sure that what I am trying to do will be clear so I won't waste time explaining it.

 

Help would be appreciated.!

Link to comment
https://forums.phpfreaks.com/topic/147282-solved-session-login/
Share on other sites

Sorry about that. Here goes...

 

<?php
if (isset($_COOKIE['username'])) //Trying to check if session exists
{
   $username = $_COOKIE['username']; // Sets variables of their username and pass
   $pass = $_COOKIE['pass'];
   $pass = md5($pass);

   $checkcookie = mysql_query("SELECT * FROM accounts WHERE username = '$username'") or die(mysql_error()); // MySQL query selects username from DB.

      if (!$checkcookie)
      {
         die('MySQL Error' . mysql_error());
      }   
   
      else
      {
      $filterresults = mysql_fetch_assoc($checkcookie); 
      $theirusername = $filterresults['username']; // Sets variables of what the query returned as their username and pass.
      $theirpass = $filterresults['password'];
      
      if($username != $theirusername | $pass != $theirpass) //Checks if the username and password in cookie are the same as in db
      {
         echo "Incorrect Something";
         
if (isset($_POST['submit'])) //IF they submit the login form
{
   if($_POST['password'])
   {
      $dbhost = "localhost";
      $dbuser = "user";
      $dbpass = "pass";
      $dbname = "game";
      $connect = mysql_connect($dbhost,$dbuser,$dbpass);
      
      if (!$connect)
      {
         die('MySQL Error ' . mysql_error());
      }
      
      else
      {
         $selectdb =   mysql_select_db($dbname);
         if (!$selectdb)
         {
            die('MySQL Error ' . mysql_error());
         }      
         else
         {
         $username = $_POST['username']; // Sets username and pass from form.
         $pass = $_POST['pass'];
         $pass = md5($pass);
      
         $checkuser = mysql_query("SELECT * FROM accounts WHERE username = '$username'") or die(mysql_error()); //Checks if username exists
         
            if (!$checkuser)
            {
               die('MySQL Error' . mysql_error());
            }   
         
            else
            {
            $filterresults = mysql_fetch_assoc($checkuser); // Sets username and pass from query.
            $theirusername = $filterresults['username'];
            $theirpass = $filterresults['pass'];
            
            if($username != $theirusername | $pass != $theirpass) // Checks if username and pass are correct.
            {
               $error = "Incorrect Username or Password";
            }
            else
            {
            session_start(); //Starts session with username and pass of what they put in the form.
            $_SESSION["username"] = $username;
            $_SESSION["pass"] = $pass; 
            echo "session created";
            }
            }
         }
      }
   }
}
}
else{
header("Location:gameindex.php"); //Sends them to gameindex.php if cookie exists and is ok.
}
}
}
?>
//HTML form..

<html>
<head>
   <title>Login - Fall of The Empire</title>
   <link rel="stylesheet" href="styles.css" type="text/css"/>
</head>
<body bgcolor="272d31">
      <div align="center" class="header">
         <img src="images/logo.png" width="473" height="227" alt="Logo">
   </div>
   <br>
   <center>
   <div class="main">
   <div class="navbar">
      <img src="images/nav_top.png" width="232" height="46" alt="Nav Top"><br>
      <a href="index.php"><img src="images/nav_home.png" width="232" height="33" alt="Nav Home" border="0"></a><br>
      <a href="news.php"><img src="images/nav_news.png" width="232" height="32" alt="Nav News" border="0"></a><br>
      <a href="about.php"><img src="images/nav_about.png" width="232" height="23" alt="Nav About" border="0"></a><br>
      <a href="login.php"><img src="images/nav_login.png" width="232" height="32" alt="Nav Login" border="0"></a><br>
      <a href="register.php"><img src="images/nav_register.png" width="232" height="25" alt="Nav Register" border="0"></a><br>
      <a href="contactus.php"><img src="images/nav_bottom.png" width="232" height="179" alt="Nav Bottom" border="0"><br></a>
      </div>   

      <div class="cb">
      <img src="images/login.png" align="right" class="welcome">
      <br>
      <br>
      <br>
      <br>
      <table width="300px" align="right">
         <form action="<?php echo $PHP_SELF; ?>" method="POST">
            <tr>
               <td>
                  <font color="white">
                     Username:
                  </font>
               </td>
               
               <td>
                  <input type="text" name="username">
               </td>
            </tr>
            
            <tr>
               <td>
                  <font color="white">
                     Password:
                  </font>
               </td>
               
               <td>
                  <input type="password" name="password">
               </td>
            </tr>
         
            <tr>
               <td>
                  <br>
               </td>
               <td>
               </td>
            <tr>
               <td>
               </td>
         <td align="right" style="padding-right:8px">
         <input type="submit" name="submit" value="Login"align="right">
         </td>
         </tr>
         </form>   
      </table>
      <p><?php echo $error; ?></p>
      </div>
      </div>
      </center>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/147282-solved-session-login/#findComment-773474
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.