FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


simple login script





riv_
I've been trying to get a simple login script going for a simple page I'm putting together, but I can't seem to make it work. I finally gave up and tried a ready few ready made scriots, and they don't work either.
Does anyone have any idea whats going on?
Can someone suggest what to try next.
I'm using a simple script that finds a username in the database that matches the user-entered string. It then compares the user-entered password to the db string, and responds according to if-else instructions.
Hardly rocket science.
No error message. But one pre made script lets everyone in, another keeps everyone out, and a third gives me a totally blank screen.
Any ideas?
I am a total newbie, and totally open to any suggestion. (But please don't refer me to the manual, I've checked there. I need HELP!)
Thanks so much!
LukeakaDanish
You probably want to tell us what language you're using, give us some code to look at etc...
vinx_18
Yes buddy...you have to tell us what language you use and probably you have to learn the basics first: syntax, etc... and do the simple login script after....
n0obie4life
Create a file that has this in it, like index.html

Code:
<html>
<form action="login.php" method="post">
Username: <input type="text" name="username" />
Enter your age: <input type="text" name="password" />
<input type="submit" />
</form>
</html>


Create a file called login.php.

Code:
<?php

$mysql_dbname = 'riv_name';
$mysql_dbusername = 'riv_user';
$mysql_dbpassword = 'iaml33thax';

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

$connect = mysql_connect('localhost', $mysql_dbusername, $mysql_dbpassword);
if (!$link)
{
   die('Could not connect to mySQL Database: ' . mysql_error());
}

$db_selected = mysql_select_db($mysql_dbname,$connect);
if (!$db_selected)
{
   die ('Failed to select database : ' . mysql_error());
}

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

if (!$result)
{
   die ('Failed to get data from table : ' . mysql_error());
}
$num_rows = mysql_num_rows($result);

if ( $num_rows == '0' )
{
   die ('Invalid Username!');
   exit;
}
else
{
   $row = mysql_fetch_row($result);
   
   if ( $password == $row[1] )
   {
      echo ' Logged in ! ';
   }
   else
   {
      echo ' Wrong Password! ';
      exit;
   }
}

?>


Remember to change these 3 values

Code:
$mysql_dbname = 'riv_name';
$mysql_dbusername = 'riv_user';
$mysql_dbpassword = 'iaml33thax';


and whatever you want after he logged in

the sql side, run this query in phpmyadmin

Code:
CREATE TABLE `users` (
  `username` varchar(25) NOT NULL,
  `password` varchar(32) NOT NULL
) TYPE=MyISAM;


Then just make a new user through phpmyadmin manually yourself.

Thescript should work, I never tested it doh.
dandelion
Why don't use the PEAR::Auth package? It seems as easy as possible. Please look below for an example.
Code:
<?php

require_once "Auth.php";

$dsn = "mysql://user:password@localhost/database";
$auth = new Auth("DB", $dsn);

$auth->start();

if ($auth->checkAuth()) {
    // output your page for registered users
}
else {
    // output your page for guests
}

?>

For further details see Auth documentation.
Rhysige
tis just as simple to put $_SESSION['loggedin'] = 1; into the section where they login on the aforementioned login script. Then on any future pages put
if($_SESSION['loggedin'] != 1){die('Not logged in'); }
And ofcourse a logout script which wipes the session, obviously you could also add user types etc to the sessions..
riv_
Thanks...
I'll try that. I've tried a few similar things, but the "else" statement has varied a bit... that looks better.
And good idea about the session login whatchamacallit.
riv_
I just wanted to say thankyou, especially to noobie4life.
Turns out, I've been handling my arrays returned from the db all wrong, and it took this example to make it click for me!
Hopefully it will be easier next time, now that 1 piece of the puzzle has fallen into place. I really appreciate the patient explanations!!
riv_
I haven't the foggiest about working with sessions... Do I just use session_start when the user logs in???
I seem to be doing OK with my bits and pieces of script... but now that I'm getting closer to having them all done, I'm honestly not sure how to fit them all together. Can anyone point me to someplace helpful for that?
Related topics
Simple login system
Login Script
Simple PHP script not working
Login Script - PHP
I need a VERY simple php script
150 frih$ if you make me a login script with....
javascript/ajax only to have login/user editor
Fix up a login script
Free PHP/MySQL login script
PHP login script problem
can i do something for you?
Servlet Basic login script?
Protect Your Page With Simple Login But Yet Powerful Script
Simple PHP Login Script
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.