Staredit Network

Staredit Network -> Computers and Technical -> PHP help required.
Report, edit, etc...Posted by Centreri on 2006-09-25 at 15:49:11
I'm working on the register/login system for my website, and finished the register part. Now I'm doing the login part. Once the person writes their username/password into the forms, it gets sent to the file 'System/login.php' and the user is redirected their and then back to the main page of the site. Here is the code in login.php:

CODE
<?php

$loginErrorMessage;
$loginValueMissingNumber = 0;
$loginValueMissing = false;

if (!isset($_POST['logUser'])) {
$loginErrorBol = true;
$loginErrorMessage = $loginErrorMessage . "Your login username field is not filled in. ";
$loginValueMissing = true;
$loginValueMissingNumber = $loginValueMissingNumber + 1;
}

if (!isset($_POST['logPass'])) {
$loginErrorBol = true;
$loginErrorMessage = $loginErrorMessage . "Your login password field is not filled in. ";
$loginValueMissing = true;
$loginValueMissingNumber = $loginValueMissingNumber + 1;
}

if($loginValueMissing == true) {
if($loginValueMissingNumber == 1) {
 $loginErrorMessage = $loginErrorMessage . "Please fill in this field. ";
}
else if ( $loginValueMissingNumber == 2) {
 $loginErrorMessage = $loginErrorMessage . "Please fill in these fields. ";
}
}

if ($loginValueMissing == true) {
$_SESSION['loginErrorMessage'] = $loginErrorMessage;
$_SESSION['loginErrorBol'] = true;
header("location: ../Mainpage.php");
} else {
$loginConnect = mysql_connect("*hostname*", "*blahblah*", "*notsaying");
mysql_select_db('centreri', $loginConnect);
$loginCheck = "SELECT * FROM userinformation WHERE Account = " . $_POST['logUser'] . " AND Password = " . $_POST['logPass'];
$loginCheckFinish = @mysql_query($loginCheck, $loginConnect);
if (!$loginCheckFinish) {
 $_SESSION['loginErrorMessage'] = "Your username and password were not found in our database.";
 $_SESSION['loginErrorBol'] = true;
 header("location: ../Mainpage.php");
} else {
 setcookie("login", $_POST['logUser'],  time()+3600);
 header("location: ../Mainpage.php");
}
}
?>


The problem is that while no error messages appear and all that, the cookie is apparently not set because the member area that displays messages and all that does not appear for me. It should be showing if you're logged in, for now in the simple format of
CODE
<?php
 // User Menu
    if (isset($_COOKIE['login'])) {
 echo("
 <tr>
  <td valign=\"top\" colspan=\"2\">
 <div>
  <div style=\"background-image: url('Images/MIBGTop39-750.png'); width: 750px; height: 39px;\">
   <a href=\"javascript: toggleLayer('userMenu');\" style=\"text-align: left;\" title=\"Hide/Show User Menu\">
   <img src=\"Images/Togglebutton.png\" style=\"cursor: pointer; border: none;\" valign=\"bottom\" />
   </a>
  </div>
  <div id=\"userMenu\">
   <div style=\"background-image: url('Images/MIBGMid64-750.png'); width: 750px; height: 79px;\"></div>
   <div style=\"background-image: url('Images/MIBGBottom22-750.png'); width: 750px; height: 22px;\"></div>
  </div>
 </div>
  </td>
 </tr>
  ");
 }
 ?>

in the main page. Any ideas of what's not working? I suspect it is the query in login.php, but I have been unable to find the proper format for that kind of stuff online. Oh, and I tried many different formats for this, about 5 of them. I wouldn't post it here if I had no idea what to do.
Report, edit, etc...Posted by brutetal on 2006-09-25 at 22:39:54
Let me play around with your code, I'll get back soon.

After a few minutes, is that the entire code?
Becuase you didn't set the session.
I'm pretty sure you have to put

session_start();

for $_SESSION veriables to work, because it doesn't work if I don't put it.
Report, edit, etc...Posted by Centreri on 2006-09-26 at 17:05:20
Rofl.. That's what I forgot to put in? One second.. Yep, that was the problem.

Now it's displaying a 'Your username and password were not found in our database.' message after I entered the Username/Password combination, checked MySQL if everything was there and the Username/Password are still in the database.

So I'm quite sure the problem is the format of the query in the first document. Any help with that?
Report, edit, etc...Posted by brutetal on 2006-09-27 at 09:54:03
I'll see what I can do.

At first glance,

$loginCheckFinish = @mysql_query($loginCheck, $loginConnect);

It might be the @ right before mysql_query.

So remove the @, and try it again. I can't right now, I need to get to school!
Report, edit, etc...Posted by Centreri on 2006-09-27 at 17:09:34
Nope, that stopped the redirection, probably to make sure that the user gets the error message when the query wasn't able to be performed.
Report, edit, etc...Posted by brutetal on 2006-09-27 at 20:33:02
Wait...
Okay Try this:

$logUser = $_POST['logUser'];
$logPass = $_POST['logPass'];
$loginCheck = 'SELECT * FROM userinformation WHERE Account =$logUser AND Password =$logPass';

Now that might work....
I know... Just try it, if it works then start thinking of a solution to it!



Off topic kinda:
You got a nice website going, but looks like you could use some help. I don't mind helping.
Report, edit, etc...Posted by Centreri on 2006-09-27 at 21:07:15
Nope. Still error.
Report, edit, etc...Posted by brutetal on 2006-09-28 at 01:47:58
what PHP version are you using?

I should of asked first. xD

Also, are u sure you typed in the correct database name, table name, or the data column names??
Report, edit, etc...Posted by Centreri on 2006-09-28 at 15:52:00
PHP Version 5.1.2

Not sure if it's the latest, but I think any v4+ should be able to work.

Everything else is correct, I checked and doublechecked.
Report, edit, etc...Posted by Syphon on 2006-09-28 at 18:49:55
Have you tried putting the mysql_Connect(); / session_Start(); at the top?
Report, edit, etc...Posted by IsolatedPurity on 2006-09-28 at 19:19:22
CODE
<?php
$loginCheck = "SELECT * FROM userinformation WHERE Account = " . $_POST['logUser'] . " AND Password = " . $_POST['logPass'];
$loginCheckFinish = @mysql_query($loginCheck, $loginConnect);


That should produce an error because you are not writting the query right.
Non-digit strings in the query need to be enclosed with ' ' 's to be valid.
So...
SELECT * FROM userinformation WHERE account='Centreri' AND password='thisismypassword' is what you want your final result to be:
$loginCheck = "SELECT * FROM userinformation WHERE Account = '" . $_POST['logUser'] . "' AND Password = '" . $_POST['logPass'] . "'";

And... one thing that isn't mentioned too much is writting variables in strings in a different but easier format. Matter of opinion, I'm sure, but constantly breaking strings and using concats and crap just gets annoying for me.

$loginCheck = "SELECT * FROM userinformation WHERE Account ='{$_POST['logUser']}' AND Password='{$_POST['logPass']}'";

So... enclose variables with { and } instead of " . and . "
It'll make your life easier smile.gif.

Oh and...
QUOTE
Nope, that stopped the redirection, probably to make sure that the user gets the error message when the query wasn't able to be performed.

The query wasn't able to be performed... sounds like you knew most of your problem smile.gif. As far as the errors not showing, I need to see more code. mainpage.php and login.php(?).

ADDITION:
And PS: Another such tip is this:

$loginErrorMessage = $loginErrorMessage . "Your login username field is not filled in. ";

=

$loginErrorMessage .= "Your login username field is not filled in. ";
Report, edit, etc...Posted by brutetal on 2006-09-28 at 21:43:03
Ahh... cool!!!

Thx IP, I shall remember this!!!
Report, edit, etc...Posted by Centreri on 2006-10-01 at 17:52:13
I knew the last part, I just feel more comfortable writing it so it's easier to read instead of using .=. Thanks for the help.

EDIT
Wow. Just wow. Still not working.

login.php:
CODE
<?php
session_start();

$_POST['logUser'];
$_POST['logPass'];

$loginErrorMessage;
$loginValueMissingNumber = 0;
$loginValueMissing = false;

if (!isset($_POST['logUser'])) {
$loginErrorBol = true;
$loginErrorMessage = $loginErrorMessage . "Your login username field is not filled in. ";
$loginValueMissing = true;
$loginValueMissingNumber = $loginValueMissingNumber + 1;
}

if (!isset($_POST['logPass'])) {
$loginErrorBol = true;
$loginErrorMessage = $loginErrorMessage . "Your login password field is not filled in. ";
$loginValueMissing = true;
$loginValueMissingNumber = $loginValueMissingNumber + 1;
}

if($loginValueMissing == true) {
if($loginValueMissingNumber == 1) {
 $loginErrorMessage = $loginErrorMessage . "Please fill in this field. ";
}
else if ( $loginValueMissingNumber == 2) {
 $loginErrorMessage = $loginErrorMessage . "Please fill in these fields. ";
}
}

if ($loginValueMissing == true) {
$_SESSION['loginErrorMessage'] = $loginErrorMessage;
$_SESSION['loginErrorBol'] = true;
header("location: ../Mainpage.php");
} else {
$loginConnect = mysql_connect("tehhost", "mah", "meh");
mysql_select_db('centreri', $loginConnect);
$loginCheck = "SELECT * FROM userinformation WHERE Account = '" . $_POST['logUser'] . "' AND Password = '" . $_POST['logPass'] . "'";
$loginCheckFinish = @mysql_query($loginCheck, $loginConnect);
if (!$loginCheckFinish) {
 $_SESSION['loginErrorMessage'] = "Your username and password were not found in our database.";
 $_SESSION['loginErrorBol'] = true;
 header("location: ../Mainpage.php");
} else {
 setcookie("login", $_POST['logUser'],  time()+3600);
 header("location: ../Mainpage.php");
}
}
 
?>


Mainpage.php:
CODE
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title> Centreri Network &raquo; Main </title>
 <link rel="stylesheet" type="text/css" href="Main.css" />
 <link rel="shortcut icon" href="Images/SpecIcon.ico" />
 <script src="System/js_Library.js" type="text/javascript"></script>
</head>
<body>
<center>
 <div class="main">
   <?php // Banner Area ?>
  <div class="bannerarea" style="text-align: left;" valign="bottom">
   <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
   <a href="javascript: toggleLayer('mainNav');" title="Hide/Show Navigation Bar">
    <img src="Images/Togglebutton.png" style="cursor: pointer; border: none;" valign="bottom" />
   </a>
  </div>
   <table cellspacing="0" style="width: 750px; text-align: left;">
    <tr>
  <td colspan="2">
   <div id="mainNav" style="height: 60px;">
    <div style="background-image: url('Images/MarqueeBG32-750.png');  width: 750px; height: 32px; text-align: center;" >
  <center>
   <div style="height: 6px;"></div>
       <marquee style="width: 716px;"> Working on making the main site of Centreri Network. This will be quick-news area.</marquee>
   <div style="height: 6px;"></div>
  </center>z
    </div>
   <?php include("navigation_Top.html"); ?>
   </div>
  </td>
 </tr>
 <?php
 // User Menu
    if (isset($_COOKIE['login'])) {
 echo("
 <tr>
  <td valign=\"top\" colspan=\"2\">
 <div>
  <div style=\"background-image: url('Images/MIBGTop39-750.png'); width: 750px; height: 39px;\">
   <a href=\"javascript: toggleLayer('userMenu');\" style=\"text-align: left;\" title=\"Hide/Show User Menu\">
   <img src=\"Images/Togglebutton.png\" style=\"cursor: pointer; border: none;\" valign=\"bottom\" />
   </a>
  </div>
  <div id=\"userMenu\">
   <div style=\"background-image: url('Images/MIBGMid64-750.png'); width: 750px; height: 79px;\"></div>
   <div style=\"background-image: url('Images/MIBGBottom22-750.png'); width: 750px; height: 22px;\"></div>
  </div>
 </div>
  </td>
 </tr>
  ");
 }
 ?>
 <?php // Side Panel ?>
 <tr>
  <td style="width: 250px; height: 500px; text-align: center;" valign="top">  
  <div style="background-image: url('Images/SideBGTop41-250.png'); height: 41px;"></div>
  <div style="background-image: url('Images/SideBGMid60-250.png');">
   <span style="width: 200px;">
    <?php
   if (isset($_SESSION['registerComplete'])) {
    if ($_SESSION['registerComplete'] == true) {
    echo("Your registration was successfully completed. Please log in below.");
 }
   }
   if (isset($_SESSION['loginErrorBol']))
    {
 echo "<div style=\"background-color: #FF0000;\"><b>Login Error</b><br />" . $_SESSION['loginErrorMessage'] . "</div>";
 }
 
   if (!isset($_COOKIE['login'])) {
   echo ("
   <form action=\"System/login.php\" method=\"post\">
    Username: <input type=\"text\" name=\"logUser\" maxlength=\"15\" /><br />
 Password: <input type=\"password\" name=\"logPass\" maxlength=\"20\" /><br />
 <input type=\"image\" value=\"Log In\" src=\"Images/LoginButton.gif\" />
   </form> <br />
   <a href=\"register.php\">Register</a>&nbsp;&nbsp;
   ");
   }
   ?>
  </span>
  </div>
  <div style="background-image: url('Images/SideBGBottom22-250.png'); height: 22px;"></div>
  </td>
  <?php // Main Content ?>
  <td style="width: 500px; heght: 500px;" valign="top">
   <div style="width:500px; height: 32px; background-image: url('Images/ContentBGTop32-500.png'); text-align: center;">
    <span style="width: 250px; text-align: center;" valign="middle">Main Content</span>
   </div>
   <div style="width: 500px; background-image: url('Images/ContentBGMid54-500.png'); text-align: center; "">
    <span style="width: 450px; text-align: left;">
  Welcome to Centreri Network. <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 </span>
   </div>
   <div style="width: 500px; height: 15px; background-image: url('Images/ContentBGBottom15-500.png');"></div>
   </td>
 </tr>
   </table>  
  </div>
 
</center></body>
</html>
<?php session_destroy(); ?>



It reverts to Mainpage.php without the changes that should come with the cookie. The 'User Menu' doesn't appear, an error doesn't appear, simple redirect back to Mainpage.php with no changes.

ADDITION:
Fixed my query. The main problem was the setcookie(), I didn't add a '/' into the function, and because it was in a folder inside the main site directory the mainpage didn't have access to the cookie.
Next Page (1)