Jump to content

Recommended Posts

Ok, I have a simple login script that I am trying to incorporate into a single page instead of using two pages. So the way I had it origionally is using two files, index.php, and checklogin.php. What I am trying to do is combine them into a single. So, basically, it would display the form, then run the checklogin.php to check the login and do what needs to be done. The files are below:

 

index.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<td>Username</td>
<td>:</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

 

 

checklogin.php

<?php
include 'lib/config.php';
include 'lib/db_open.php';

// username and password sent from signup form
$user=$_POST['username'];
$pass=$_POST['password'];

$sql="SELECT * FROM $tbl_login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $user and redirect to users location
session_register("username");
header("location:index2.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

and this is the page i am trying to combine then into...

 

login.php

<?php
include 'lib/config.php';
include 'lib/db_open.php';

// username and password sent from signup form
$user=$_POST['username'];
$pass=$_POST['password'];

$sql="SELECT * FROM $tbl_login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $user and redirect to users location
session_register("username");
header("location:index2.php");
} elseif($count!=1) {
echo "<div style='text-align:center'>";
echo "<form name='form1' method='post' action='<?php echo $PHP_SELF;?>'>";
echo "<table border='0' cellpadding='3' cellspacing='3' style='padding-top:15px;margin:0 auto;'>";
echo "<tr>";
echo "<td>Username:</td>";
echo "<td><input name='username' id='username' type='text' size='20'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password:</td>";
echo "<td><input name='password' id='password' type='password' size='20'></td>";
echo "</tr>";
echo "<tr align='right'>";
echo "<td colspan='2'><input name='Login' type='submit' id='Login' value='  Login  '></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
} else {
echo "<b>Error:</b> Wrong Username or Password";
}
?>

 

How would I do this? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/43848-login-script/
Share on other sites

try something like this:

<?php 

function login(){
include 'lib/config.php';
include 'lib/db_open.php';

// username and password sent from signup form
$user=$_POST['username'];
$pass=$_POST['password'];

$sql="SELECT * FROM $tbl_login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $user and redirect to users location
session_register("username");
header("location:index2.php");
} elseif($count!=1) {
echo "<div style='text-align:center'>";
echo "<form name='form1' method='post' action='<?php echo $PHP_SELF;?>'>";
echo "<table border='0' cellpadding='3' cellspacing='3' style='padding-top:15px;margin:0 auto;'>";
echo "<tr>";
echo "<td>Username:</td>";
echo "<td><input name='username' id='username' type='text' size='20'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password:</td>";
echo "<td><input name='password' id='password' type='password' size='20'></td>";
echo "</tr>";
echo "<tr align='right'>";
echo "<td colspan='2'><input name='Login' type='submit' id='Login' value='  Login  '><input type=\"hidden\" name=\"action\" value=\"checklogin\"></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
} else {
echo "<b>Error:</b> Wrong Username or Password";
}
}

function checklogin(){
include 'lib/config.php';
include 'lib/db_open.php';

// username and password sent from signup form
$user=$_POST['username'];
$pass=$_POST['password'];

$sql="SELECT * FROM $tbl_login WHERE username='$user' and password='$pass'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $user and redirect to users location
session_register("username");
header("location:this_script.php"); // can also be header("location:this_script.php?action=login");
}
else {
echo "Wrong Username or Password";
}

}

switch ($action){

case "login":
	login();
	break;

case "checklogin":
	checklogin();
	break;

default:
	login();
	break;
}

?>

 

 

*** notice the hidden value in your login form

 

hope this helps

Link to comment
https://forums.phpfreaks.com/topic/43848-login-script/#findComment-212879
Share on other sites

Hmm. Well, see, while I do want it combined into a single page, the problem is with doing that, is actually displaying the login form. Before, the login form was on a separate page, and then the action would take the information entered from the form and either redirect you if it is correct or display an error message if it is wrong. However, when I try to combine them, it becomes difficult because I am not sure how to get the login form to display THEN do the check after the submit button is pressed. Because what I mean is, origionally its as simple is if it works then go, if not, then error. But now, the problem is, as soon as someone opens up the page, the php runs, and because they never entered anything into the form yet, its detecting it as being inaccurit information and displaying the error message AND the form. What I want, it to display the login form first, THEN IF, the information is correct go, ELSE IF the information is incorrect then display the error message. Do you know what I am getting at?

Link to comment
https://forums.phpfreaks.com/topic/43848-login-script/#findComment-212893
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.