Jump to content

Login To Database Problem


iNko

Recommended Posts

Hi, i uploaded my database to a server and now im just trying to connect to it

 

this is my config file:

<?php
$con = mysql_connect ("my server details", "my server details", "my server details");
if (!$con)
{
die('Message if connection was bad: ' . mysql_error());
}
mysql_select_db("my database name");
?>

 

And this is my login form code:


<?php
require('connectdatabase.php'); //takes config file info

if(isset($_POST['submit'])) {
$login = mysql_escape_string($_POST['login']);
$password = mysql_escape_string($_POST['password']);
$sql = mysql_query("SELECT * FROM `Vartotojas` WHERE `login` = '$login' AND `password` = '$password'");

if(mysql_num_rows($sql) > 0){
include('index.php');

exit();
}else{
echo "Bad login/password";
}
}else{

?>
<form action="loginform.php" method="POST">
Login: <input type="text" name="login" /> <br/>
Password: <input type="text" name="password" /> <br/>
<input type="submit" name="submit" value="Login" />
</form>
<?php
}
?>

 

This code works but i dont even need to login to connect to my index.php (i mean i can just type /index.php and it will go to that page bypassing the loginform.php)

How do i make it so i could connect to index.php only when i go through loginform.php? (Do i need to add some code in my index.php so that it would check if im logged in?)

Also, im not sure what this part does in this code :

"if(mysql_num_rows($sql) > 0)"

Edited by iNko
Link to comment
Share on other sites

(Do i need to add some code in my index.php so that it would check if im logged in?)

Also, im not sure what this part does in this code :

"if(mysql_num_rows($sql) > 0)"

 

How else do you expect index.php to know if you are logged in or not if it doesn't check?

 

That bit of code is saying that if there was 1 or more records returned by your query, then do something (in your case : include the code that is in index.php on this page).

 

Look into using sessions

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.