Jump to content

[SOLVED] login if statement help


L

Recommended Posts

Hello, I am having problems with making the login form disappear when a user logins. I want it so when the user session is registered, links, and a login text will display instead of the form. Here is my code

<?php

session_start();
if (ISSET($_POST['sublogin']))
{
// Recreation of variables for later encryption uses the $_POST  will be replaced with the decrypted source
$username = trim($_POST['username']);
$password =  trim($_POST['password']);
$cryptpassword = md5($password);
$url = 'cp.php?user=$_SESSION[username]';
//Connects to DB
require("database.php");
$table = "users";

$sql="SELECT username, userID FROM $table WHERE username='$username' and password='$cryptpassword'";
$result = mysql_query($sql)or die(mysql_error());

// If result matched $myusername and $mypassword, table row must be 1 row
if(mysql_num_rows($result) > 0)
{
// Registers sesions and redirect to file "login_success.php"

$storage = mysql_fetch_assoc($result);

//Sessions here
   $_SESSION['username'] = $storage['username'];
   $_SESSION['userid'] = $storage['userID'];
header("location: $url");

    echo "Logged In<br />";
   echo "Welcome <b>$_SESSION[username]</b>, you are logged in. <br><br>"
       ."<a href=\"cp.php?user=$_SESSION[username]\">My Account</a><br />   ";
       echo "<a href=\"logout.php\">Logout</a>";
   }
   else{
}
else 	
{
echo "Wrong Username or Password";
}
}
?>

<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td></tr><tr><td><input type="text" name="username" size="15" maxlength="30"/></td></tr>
<tr><td>Password:</td></tr><tr><td><input type="password" name="password" size="15" maxlength="30"></td></tr><tr><td>
<input type="submit" name="sublogin" value="Login" style="font-size: 8pt; color: #000000; word-spacing: 0; margin-top: 0; margin-bottom: 0" /></td></tr>
</table>
</form>

Thank you for your time,

~L

Link to comment
https://forums.phpfreaks.com/topic/57483-solved-login-if-statement-help/
Share on other sites

<?php

   if((!isset($_SESSION['username'])) | (!isset($_SESSION['userid']))) {
  
     echo'
     <form action="" method="post">

     <table align="left" border="0" cellspacing="0" cellpadding="3">
       <tr><td>Username:</td></tr><tr><td><input type="text" name="username" size="15" maxlength="30" /></td></tr>
       <tr><td>Password:</td></tr><tr><td><input type="password" name="password" size="15" maxlength="30" /></td></tr>
       <tr><td><input type="submit" name="sublogin" value="Login" /></td></tr>
     </table>

     </form>';
   }

?>

 

 

hmm...it's not working...now the form isn't showing up when im not logged in. here's the code

<?php

session_start();
if (ISSET($_POST['sublogin']))
{
// Recreation of variables for later encryption uses the $_POST  will be replaced with the decrypted source
$username = trim($_POST['username']);
$password =  trim($_POST['password']);
$cryptpassword = md5($password);
$url = '/cp.php?user=$_SESSION[username]';
//Connects to DB
require("database.php");
$table = "users";

$sql="SELECT username, userID FROM $table WHERE username='$username' and password='$cryptpassword'";
$result = mysql_query($sql)or die(mysql_error());

// If result matched $myusername and $mypassword, table row must be 1 row
if(mysql_num_rows($result) > 0)
{
// Registers sesions and redirect to file "login_success.php"

$storage = mysql_fetch_assoc($result);

//Sessions here
   $_SESSION['username'] = $storage['username'];
   $_SESSION['userid'] = $storage['userID'];
   
header("location: $url");
}
else 	
{
echo "Wrong Username or Password";
}
}


   if((!isset($_SESSION['username'])) || (!isset($_SESSION['userid']))) {
  
     echo'
     <form action="" method="post">

     <table align="left" border="0" cellspacing="0" cellpadding="3">
       <tr><td>Username:</td></tr><tr><td><input type="text" name="username" size="15" maxlength="30" /></td></tr>
       <tr><td>Password:</td></tr><tr><td><input type="password" name="password" size="15" maxlength="30" /></td></tr>
       <tr><td><input type="submit" name="sublogin" value="Login" /></td></tr>
     </table>

     </form>';
}
?>


<?php

session_start();
if (ISSET($_POST['sublogin']))
{
// Recreation of variables for later encryption uses the $_POST  will be replaced with the decrypted source
$username = trim($_POST['username']);
$password =  trim($_POST['password']);
$cryptpassword = md5($password);
$url = '/cp.php?user=$_SESSION[username]';
//Connects to DB
require("database.php");
$table = "users";

$sql="SELECT username, userID FROM $table WHERE username='$username' and password='$cryptpassword'";
$result = mysql_query($sql)or die(mysql_error());

// If result matched $myusername and $mypassword, table row must be 1 row
if(mysql_num_rows($result) > 0)
{
// Registers sesions and redirect to file "login_success.php"

$storage = mysql_fetch_assoc($result);

//Sessions here
   $_SESSION['username'] = $storage['username'];
   $_SESSION['userid'] = $storage['userID'];
   
header("location: $url");
}
else 	
{
echo "Wrong Username or Password";
}
}


   if((empty($_SESSION['username'])) || (empty($_SESSION['userid']))) {
  
     echo'
     <form action="" method="post">

     <table align="left" border="0" cellspacing="0" cellpadding="3">
       <tr><td>Username:</td></tr><tr><td><input type="text" name="username" size="15" maxlength="30" /></td></tr>
       <tr><td>Password:</td></tr><tr><td><input type="password" name="password" size="15" maxlength="30" /></td></tr>
       <tr><td><input type="submit" name="sublogin" value="Login" /></td></tr>
     </table>

     </form>';
}
?>

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.