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
https://forums.phpfreaks.com/topic/103553-php-login/
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
https://forums.phpfreaks.com/topic/103553-php-login/#findComment-530355
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.