Jump to content

I have created a basic login using PHP, but how do i link it to another page?


usman07

Recommended Posts

I have created a login page, but i want the users to be directed to another page only if the login details are correct. How would i do this?any help is appreciate. Coding for the PHP is below: thank you.

 


<?php

$username = $_POST["username"];
$password = $_POST["password"];
//This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form.
if(isset($username)){

if (!($username == " " && $password == " "))

{

$connect = mysql_connect("","","") or die("Couldn't connect!");
mysql_select_db("") or die("Couldn't find db");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$row = mysql_fetch_array($query);

$numrows = mysql_num_rows($query);

   $dbusername = $row['username']; 
   $dbpassword = $row['password'];
}
else {
echo ('<div id="username">
   <form action="" method="post"/>
	<font color="red"> Please enter a username and password </font>
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');
}
//check to see if they match
if ($username == $dbusername && $password == $dbpassword) {
echo "You Are Now Logged In, Welcome To AdobeTuts!";
}
else
  	echo ('<div id="username">
   <form action="" method="post"/>
	<div id="new">
	<font color="red"> Wrong Username Or Password, Please Try Again </font> </div> 
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');

}

//This next bit echoes the login form unless the $username variable is set.
else {
echo ('<div id="username">
   <form action="" method="post"/>
    
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>
      
      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>
      
      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>
      
      <p class="register">Not yet a member? <a href="imgs/Homepage tuts/Form.php">Register Here</a>, its Free!</p>
</div>');
}

?>


Link to comment
Share on other sites

What I'd do is inside the if() here:

 

if (!($username == " " && $password == " "))

 

I'd place the following if statement.

 

if($username == $db_username && $password == $db_password){

     header( 'Location: http://www.yoursite.com/new_page.html' ) ;
}

Link to comment
Share on other sites

Im new to php but im doing something wrong,iv done it like this but getting a syntax error?

 


//check to see if they match
if ($username == $dbusername && $password == $dbpassword) {

header( 'Location: http://adobetuts.netai.net/log in success.html' ) ;
}

echo "You Are Now Logged In, Welcome To AdobeTuts!";
}


Link to comment
Share on other sites

iv replaced the if statement but it says: Parse error: syntax error, unexpected '}' in /home/a3073051/public_html/index.php on line 125

 

this is what i did with the code:

 


//check to see if they match
if($username == $db_username && $password == $db_password){

     header( 'Location: http://www.yoursite.com/new_page.html' ) ;
}
echo "You Are Now Logged In, Welcome To AdobeTuts!";


Link to comment
Share on other sites

This is how it should look.

 

if (!($username == " " && $password == " ")){

if ($username == $dbusername && $password == $dbpassword) {

            header( 'Location: http://adobetuts.netai.net/log in success.html' ) ;
}
echo "You Are Now Logged In, Welcome To AdobeTuts!";
} 

Link to comment
Share on other sites

I also wanted to point out, that unless the echo statement "You are now logged in, welcome to adobetuts!" is on (or referenced by a session) the page you're redirecting them too, the user won't see it  because the code will immediately redirect them away from that page.

 

I'd place that echo statement at the end in an else statement and tell the user they're NOT logged in if their username or password evaluates to false.

Link to comment
Share on other sites

ok i copied the code above, now when i type in the correct user logins it comes up with another page (which i wanted), but now when i type in the wrong user details it doesnt come up with a error message instead it comes up with a message saying You Are Now Logged In, Welcome To Adobetuts!

Link to comment
Share on other sites

Lol, read my previous post above your last one. It explains why.

 

Essentially, you're telling a failed request that it's actually a positive login. Change it to something like echo "Sorry. Incorrect user login details. Please try again.";  or whatever you prefer.

 

if (!($username == " " && $password == " ")){

if ($username == $dbusername && $password == $dbpassword) {

            header( 'Location: http://adobetuts.netai.net/log in success.html' ) ;
} else {

echo "Login information incorrect. Please try again.";
} 

Link to comment
Share on other sites

Lol, you keep beating me to the punch dang it! :D.

 

Use the code I posted above and you should be fine. You can also use another redirect in that page or have the page refresh itself (which is what I do for long forms keeping all inputted values except for passwords etc.).

Link to comment
Share on other sites

this is what i have just in this little section of the php code:

 


if (!($username == " " && $password == " ")){

if ($username == $dbusername && $password == $dbpassword) {

header( 'Location: http://adobetuts.netai.net/log in success.html' ) ;
}
echo "You Are Now Logged In, Welcome To AdobeTuts!";

} else
  	echo ('<div id="username">
   <form action="" method="post"/>
	<div id="new">
	<font color="red"> Wrong Username Or Password, Please Try Again </font> </div> 
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');


[/code

Link to comment
Share on other sites

K, I see what your problem is. Here's the fixed code with notes on where I fixed it.

 

if (!($username == " " && $password == " ")){

if ($username == $dbusername && $password == $dbpassword) {

          header( 'Location: http://adobetuts.netai.net/log in success.html' ) ;
       }
} else  {                                                                                                   <===== REMOVED EXTRA } and echo statement.
  	echo ('<div id="username">
   <form action="" method="post"/>
	<div id="new">
	<font color="red"> Wrong Username Or Password, Please Try Again </font> </div> 
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');

}

 

 

UPDATE: Oops, I saw that I forgot the elses' {} braces (which I use, not completely required but keeps code nice and encapsulated which I like.)

Link to comment
Share on other sites

yeah sorry i have quite abit of code, showing it all below:

 


<?php

$username = $_POST["username"];
$password = $_POST["password"];
//This if statement asks if the $username variable is set. If it is it executes the php script. Otherwise it echoes the login form.
if(isset($username)){

if (!($username == " " && $password == " "))

{

$connect = mysql_connect("","","") or die("Couldn't connect!");
mysql_select_db("") or die("Couldn't find db");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$row = mysql_fetch_array($query);

$numrows = mysql_num_rows($query);

   $dbusername = $row['username']; 
   $dbpassword = $row['password'];
}
else {
echo ('<div id="username">
   <form action="" method="post"/>
	<font color="red"> Please enter a username and password </font>
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');
}
//check to see if they match
if (!($username == " " && $password == " ")){

if ($username == $dbusername && $password == $dbpassword) {

            header( 'Location: http://adobetuts.netai.net/log in success.html' ) ;
} else {

echo "Login information incorrect. Please try again.";
} 
} else
  	echo ('<div id="username">
   <form action="" method="post"/>
	<div id="new">
	<font color="red"> Wrong Username Or Password, Please Try Again </font> </div> 
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>

      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>

      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>

      <p class="register">Not yet a member? <a href="Form.html">Register Here</a>, its Free!</p>
</div>');

}

//This next bit echoes the login form unless the $username variable is set.
else {
echo ('<div id="username">
   <form action="" method="post"/>
    
      <table><tr><td>
      <img src="imgs/Log In/username.png" alt=""/>
      </td><td>
      <input type="text" size="30" name="username" style="background-color:transparent;" />
      </td></tr></table>
      
      <table><tr><td>
      <img src="imgs/Log In/password.png" alt=""/>
      </td><td>
      <input type="password" name="password" size="30" />
      </td></tr></table>
      
      <form id="submitb" action="">
      <input type="submit" value="Log in" />
      </form>
      
      <p class="register">Not yet a member? <a href="imgs/Homepage tuts/Form.php">Register Here</a>, its Free!</p>
</div>');
}

?>


Link to comment
Share on other sites

Seems you got quite a bit of repetitive code making your work more hard.

 

Why not just use isset() and empty() and evaluate once to see if the submit button has been and the username and password fields aren't empty. That way if submit has NOT been set, then it will show the username and password fields to log in, if it HAS been set, it will evaluate to see if both username and password fields aren't empty and go from there. It'll also be easier to add error messages if submit was set but either the password / username are empty or wrong.

 

Less code to write. here's an example.

 


// Place your SQL query in a different file and call it INTO this page using include_once('url/to/mysqlquery.php') at the very top of your page just below the <?php tag.

//Place this at the top of your page or beginning of your PHP form and hide $_POST globals inside to to kill any undefined errors.

if(isset($_POST['submit']) && !empty($_POST['username']) && !empty($_POST['password'])){

$username = $_POST['username'];
$password = $_POST['password'];

    if($username == $dbusername && $password == $dbpassword){
              header ('url.to.your.page.php');
     } else {
          echo "error message here with form HTML code here.";
        }   

} else {
   echo "form html code here and have form resubmit to self (action = "same_page_url.php")";
}

 

Something like that will be much, much easier to write and easier to maintain.

Link to comment
Share on other sites

I appreciate ur help, thing is someone helped me with that php code, i dont reli understand that well, and obviously what u saying is right, but i wont reli be able to do it cuz i wont know where to start or really understand that well and with the time frame i have, i wont have time to try to understand it.

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.