Jump to content

pre-defined login


solhunter

Recommended Posts

Hi again, I also have another question.

 

I have another two pages that act as a login and the login page. This is the log.php page.

 

<form action="welcome.php" method="post">

 

User Name: <input type="text" name="name">

Password:  <input type="password" name="password">

<input type="submit" value="Login">

 

<?php

 

$name = $_POST["name"];

$pass = $_POST["password"];

 

if ($name && $pass == "admin")

 

header('Location: ../welcome.php');

else

header('Location: ../log.php');

 

?>

 

</form>

 

 

and this is the welcome.php page.

 

<h2><center>Welcome <?php echo $_POST["name"]; ?>!</center></h2>

 

<a href=next.php>Site 1

<a href=admin.php>Admin Site

 

 

 

Basically i want a pre defined user with the password being the same, both "admin". If someone enters admin in both fields its suppose to redirect to the welcome page, if not it go's back to the login. Im not sure exactly what file i should put the first piece of php code.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/
Share on other sites

Actually, you're going to need to change it around a bit more than that. Try changing welcome.php to:

 

<?php
if(isset($_POST['submit']){//lets see if the form has been submitted before we try and use the variables


$name = $_POST["name"];
$pass = $_POST["password"];

if ($name == "admin" && $pass == "admin")
   header('Location: ../welcome.php');
else
   header('Location: ../log.php');

}
?>
<form action="welcome.php" method="post">
User Name: <input type="text" name="name">
Password:  <input type="password" name="password">
<input type="submit" value="Login" name="submit">
</form>

 

The reason for the changes: You cannot send a header after output. Therefore, we want to do the direction before the form is displayed. I also added in a check to see if the form has been submitted. Otherwise you would always be redirected, everytime the page loads.

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/#findComment-376860
Share on other sites

Actually, you're going to need to change it around a bit more than that. Try changing welcome.php to:

 

<?php
if(isset($_POST['submit']){//lets see if the form has been submitted before we try and use the variables


$name = $_POST["name"];
$pass = $_POST["password"];

if ($name == "admin" && $pass == "admin")
   header('Location: ../welcome.php');
else
   header('Location: ../log.php');

}
?>
<form action="welcome.php" method="post">
User Name: <input type="text" name="name">
Password:  <input type="password" name="password">
<input type="submit" value="Login" name="submit">
</form>

 

The reason for the changes: You cannot send a header after output. Therefore, we want to do the direction before the form is displayed. I also added in a check to see if the form has been submitted. Otherwise you would always be redirected, everytime the page loads.

 

 

Thanks GingerRobot! I tried what you told me to do, it loads fine but if something other then "admin" is entered for the user and password is still logs in successfully. Any idea on what it could be?

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/#findComment-376984
Share on other sites

Can you show the code you are actually using?

 

<?php

 

if( isset($_POST['submit']) ){

 

$name = $_POST["name"];

$pass = $_POST["password"];

 

if ($name == "admin" && $pass == "admin")

 

header('Location: ../assn1/welcome.php');

else

header('Location: ../assn1/login.php');

echo "Login error. Please log in again.";

 

}

?>

 

<html>

<head>

<title>Welcome Please Login</title>

</head>

 

<body bgcolor = "6699CC">

 

<center>Welcome, Please login.</center>

 

<form action="welcome.php" method="post">

 

User Name: <input type="text" name="name"><br><br>

 

Password: <input type="password" name="password"><br><br>

 

<input type="submit" value="Login" name="submit">

 

 

</form>

</body>

</html>

 

 

 

 

 

 

AND welcome.php:

 

 

 

 

<html>

<body bgcolor = "6699CC">

 

<h2><center>Welcome <?php echo $_POST["name"]; ?>!</center></h2>

 

<a href=store.php>Store Site<br>

<a href=admin.php>Admin Site<br>

 

 

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/#findComment-376989
Share on other sites

the problem lies in the login.php

<form action="welcome.php" method="post">

 

you want them to go to your welcome.php in any case, not want them to go login page

you should change it to

<form action="log.php" method="post">

 

 

 

also, you changed the redirection of the else statement  u changed to login.php, original is log.php

in fact, if same page, why u use redirection????

u can just delete that redirection

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/#findComment-376997
Share on other sites

the problem lies in the login.php

<form action="welcome.php" method="post">

 

you want them to go to your welcome.php in any case, not want them to go login page

you should change it to

<form action="log.php" method="post">

 

 

 

also, you changed the redirection of the else statement  u changed to login.php, original is log.php

in fact, if same page, why u use redirection????

u can just delete that redirection

 

That works for the login part but when it takes me to welcome.php the value 'name' isn't recognized because the other page isn't being pointed to welcome.php anymore.

 

 

<h2><center>Welcome <?php echo $_POST["name"]; ?>!</center></h2>

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/#findComment-376999
Share on other sites

Ok Thanks for your help everyone so far, it seems to be working fine except for one thing now.

 

 

<?php

 

if( isset($_POST['submit']) ){

 

$name = $_POST["name"];

$pass = $_POST["password"];

 

if ($name == "admin" && $pass == "admin")

 

  header('Location: ../assn1/welcome.php');

else

  echo "Login error. Please log in again.";

 

}

?>

 

<html>

<head>

<title>Welcome Please Login</title>

</head>

 

<body bgcolor = "6699CC">

 

<center>Welcome, Please login.</center>

 

<form action="login.php" method="post">

 

User Name: <input type="text" name="name">

 

 

 

Password: <input type="password" name="password">

 

 

 

<input type="submit" value="Login" name="submit">

 

 

</form>

</body>

</html>

 

 

 

 

 

 

AND welcome.php:

 

 

 

 

<html>

<body bgcolor = "6699CC">

 

<h2><center>Welcome <?php echo $_POST["name"]; ?>!</center></h2>

 

<a href=store.php>Store Site

 

<a href=admin.php>Admin Site

 

 

 

</body>

</html>

 

 

 

 

When it logs in successfully it gives an error message at the top

Welcome Notice: Undefined index: name in /.automount/...

 

Im wondering if theres any way to call the username input from the login page now because it doesn't seem to be calling it.

Link to comment
https://forums.phpfreaks.com/topic/74559-pre-defined-login/#findComment-377032
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.