Jump to content

PHP Login


bravo81

Recommended Posts

Hi all,

Im trying to create an Login System.

 

the Login.php works as FAR as I know, because it moves onto index.php

But it seems to login then logout on index.php

 

Heres my source code..

 

login.php:

<?
session_start();
include './includes/db_connect.php';
if (isset($_SESSION['username'])){
header("Location: index.php");
exit();
}

if ($_POST['Submit'] && strip_tags($_POST['username']) && strip_tags($_POST['password'])){
$username = addslashes(strip_tags($_POST['username'])); 
$password = addslashes(strip_tags($_POST['password']));

$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' LIMIT 1"); 


$login_check = mysql_num_rows($sql); 
$inf = mysql_fetch_object($sql); 
if ($login_check == "0"){
$message="Error - Information Invalid";
}elseif ($login_check != "0"){

header("Location: index.php"); 
   
} else { 
    $message= "You could not be logged in.<br />"; 
}}

?>

<table width="221" border="0" align="center" cellpadding="0" cellspacing="0" class="login">
  <tr>
    <td>
    <p align="center" class="login">
    <?
echo $message;
?>
<form action="" method="post">
  <div align="center">Username:<br />
      <input name="username" type="text" id="username" value="<?php if (strip_tags($_GET['l'])){ echo "$l"; } ?>" size="15" maxlength="40">
      <br />
      <br />
    Password:<br />
  <input name="password" type="password" id="password2" value="<?php if (strip_tags($_GET['pw'])){ echo "$pw"; } ?>" size="15" maxlength="40">
  <br />
  <br />
  <input type="submit" name="Submit" value="Login">
  </div>
</form>
</p>    <div align="center"></div></td>
  </tr>
</table>

 

Index.php:

 

<?
session_start();
include './includes/db_connect.php';
include './includes/counter.php';
include './includes/functions.php';

$username=$_SESSION['username'];

$query2=mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1");
$fetch2=mysql_fetch_object($query2);

$query=mysql_query("SELECT * FROM display WHERE active='1'");

?>

<? // Start Main Content // ?>
<br><br><br>
<div align="center">
<?
while ($row=mysql_fetch_array($query)){
echo " ".$row['title']."\r\n<br> <p>".$row['content']."</p>";
}

if ($fetch2->userlevel == "1"){

echo"<b><a href=edit.php><img src=./images/edit.gif border=0></a>";
}
?>

 

The "userlevel=1" bit dont come up after login, thats why I think its logging out.

 

Im not a PHP pro so im not sure, so anyhelp would be appreciated!!

 

URL is:

http://southernheavenflorida.com/newshf

 

 

Thanks in advance,

 

Dean,

Link to comment
Share on other sites

$_SESSION['username'] isn't defined in login.php

 

Learning simple debugging practices will help solve these on your own. Next time, try echo'ing the variables you expect a certain value from to make sure they actually contain it.

Link to comment
Share on other sites

I had troubles with this when I was starting out, I'm still not a pro but yeah what they were saying is you were trying to use your $_SESSION['username'] before it had any contents so the script wouldnt have worked. Before you write a script I've always found the best way to get a script flowing nicely is to do one section of it and echo ALL of the variables I've defined to make sure there correct then work with that.

 

I'm self teaching as well I'm now working with OOP thats fun I tell you *sigh* haha

 

Good luck with your future PHP'ing!

Link to comment
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.