dave_55 Posted April 1, 2008 Share Posted April 1, 2008 Hi everyone, Could someone look at my code please, ive created a simple login page but it doesn't seem to be working as it should. I get my first page up which asks for your login details. i enter my correct details that i have already stored in my sql table but it won't display the login success page or the password incorrect page if its wrong.....All it does is goes to the checklogin.php and then nothin is displayed, it doesn't give me errors in my browser either so i don't know whats wrong?????? Can anyone help please???? My Code: Login.php: <head> <title>login page</title> <style type = "text/css"> #loginform { border 2px solid #600; background-colour: #FFC; width: 280px } forms { margins: 5px; } label { display: block; width: 90px; float: left; clear: both; } label, input { margin-bottom: 4px; } </style> </head> <body> <div id ="loginform"> <form method="post" action="checklogin.php" name="form1"> <label for = "username">Username:</label> <input type="text" name="myusername" id="username" /> <label for = "password">Password:</label> <input type="text" name="mypassword" id="password" /> <input type="submit" name="submit" value="Login" /> </div> </body> </html> Checklogin.php <? $host = "localhost"; //login to the server $username = "dave"; //username login $password = "*****"; //password login $db_name = "test site"; //database name change to lincoln $tbl_name = "members"; //members tabel mysql_connect($host, $username, $password) or die("can't connect"); mysql_select_db($dbname) or die("Could not select lincoln rugby"); $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result = mysql_query($sql); $count = mysql_num_rows ($result); if ($count==1) { session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> login_success.php <? session_start(); if (!session_is_registered(myusername)) { header ("location:login.php"); } ?> <html> <head><title>welcome</title>title> </head> <body> <h1>Login Successsful</h1> <p> <a href="logout.php">Log Out!</a>a></p> </body> </html> logout.php <? session_start(); session_destroy(); ?> <html> <head><title>Thanks</title> </head> <body> <h1>You've Logged Out</h1> </body> </html> Quote Link to comment Share on other sites More sharing options...
Xil3 Posted April 1, 2008 Share Posted April 1, 2008 Add the following to the top of checklogin.php: ini_set('display_errors', 1); error_reporting(E_ALL | E_STRICT); session_start(); The first 2 lines should display any errors (which will allow you to narrow down any other issues) and the last line is required at the top of checklogin.php since you are using session_register in it. Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 try using <?php session_start(); // Check user inputs against database. mysql_select_db($database_db, $db); $query_check = sprintf("SELECT * FROM $tbl_name WHERE username = '$myusername'"); $check = mysql_query($query_check, $db) or die(mysql_error()); $row_check = mysql_fetch_assoc($check); $totalRows_check = mysql_num_rows($check); // If the passwords match. if($mypassword == $row_check['password']) { session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> instead of <?php $sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result = mysql_query($sql); $count = mysql_num_rows ($result); if ($count==1) { session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> also i see that ur $db_name = "test site"; try something with out a space edit: i edited my code to inlcude the session_start(); please try it again Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 found another problem mysql_select_db($dbname) or die("Could not select lincoln rugby"); change the $dbname to $db_name Quote Link to comment Share on other sites More sharing options...
dave_55 Posted April 1, 2008 Author Share Posted April 1, 2008 Hi are you saying I need to change this in my checklogin.php page and give that a go??? Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 try just changing the $dbname to $db_name first then let me know Quote Link to comment Share on other sites More sharing options...
dave_55 Posted April 1, 2008 Author Share Posted April 1, 2008 Just changed that error but its still working like it was before? Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 alirght replace checklogin php with this (make sure to re edit your pw's and names and stuff, also try to remove spaces in the database name) <?php $host = "localhost"; //login to the server $username = "dave"; //username login $password = "*****"; //password login $db_name = "test site"; //database name change to lincoln $tbl_name = "members"; //members tabel $db = mysql_pconnect($host, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); // Check user inputs against database. $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; session_start(); mysql_select_db($db_name, $db); $query_check = sprintf("SELECT * FROM $tbl_name WHERE username = '$myusername'"); $check = mysql_query($query_check, $db_name) or die(mysql_error()); $row_check = mysql_fetch_assoc($check); $totalRows_check = mysql_num_rows($check); // If the passwords match. if($mypassword == $row_check['password']) { session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 1, 2008 Share Posted April 1, 2008 ok first if u are getting a blank page, check error.log in the apache folder, if no errors then try this: Checklogin.php <?php session_start(); $host = "localhost"; //login to the server $username = "dave"; //username login $password = "*****"; //password login $dbname = "lincoln"; //database name change to lincoln $tbl_name = "members"; //members tabel mysql_connect($host, $username, $password) or die("can't connect: ".mysql_error()); mysql_select_db($dbname) or die("Could not select lincoln rugby: ".mysql_error()); $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $sql = "SELECT `username` FROM `$tbl_name` WHERE `username`='".mysql_escape_string($myusername)."' and `password`='".mysql_escape_string($mypassword)."'"; $result = mysql_query($sql) or die("Error in query ($sql): ".mysql_error(); if (mysql_num_rows ($result) >= 1) { $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['mypassword'] = $_POST['mypassword']; header("location: login_success.php"); } else { echo "Wrong Username or Password"; } ?> hope this helps, Quote Link to comment Share on other sites More sharing options...
dave_55 Posted April 1, 2008 Author Share Posted April 1, 2008 ok changed it to this: and getting this error: Warning: mysql_pconnect() [function.mysql-pconnect]: Access denied for user 'dave'@'localhost' (using password: YES) in C:\wamp\www\test site\checklogin.php on line 10 Fatal error: Access denied for user 'dave'@'localhost' (using password: YES) in C:\wamp\www\test site\checklogin.php on line 10 <?php $host = "localhost"; //login to the server $username = "dave"; //username login $password = "ozxoka"; //password login $db_name = "logintest"; //database name change to lincoln $tbl_name = "members"; //members tabel $db = mysql_pconnect($host, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); // Check user inputs against database. $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; session_start(); mysql_select_db($db_name, $db); $query_check = sprintf("SELECT * FROM $tbl_name WHERE username = '$myusername'"); $check = mysql_query($query_check, $db_name) or die(mysql_error()); $row_check = mysql_fetch_assoc($check); $totalRows_check = mysql_num_rows($check); // If the passwords match. if($mypassword == $row_check['password']) { session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 1, 2008 Share Posted April 1, 2008 make sure the username / password is correct and exists for mysql. mysql cannot authenticate your username / password. we cant help there you need to get the info. Quote Link to comment Share on other sites More sharing options...
dave_55 Posted April 1, 2008 Author Share Posted April 1, 2008 ok fixed that had a capital D instead of d on my username....now thats done i get thiserror from the code.... Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\test site\checklogin.php on line 19 Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 1, 2008 Share Posted April 1, 2008 either try my code OR: $check = mysql_query($query_check, $db_name) or die(mysql_error()); may need to be: $check = mysql_query($query_check) or die(mysql_error()); hope this helps, Quote Link to comment Share on other sites More sharing options...
dave_55 Posted April 1, 2008 Author Share Posted April 1, 2008 Thats great thanks for your help. I changed it how you said and its fine now. Thanks again Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 fix $check = mysql_query($query_check, $db_name) or die(mysql_error()); with $check = mysql_query($query_check, $db) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 your welcome and sorry for the sloppy code on the first time this is my first time helping any one so ya Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 1, 2008 Share Posted April 1, 2008 Actually tbh i should of told him what the error means and let him figure it out rather than give him the answer, your sloppy code is what we actually should give out to people who are new to this, they need to learn, they wont learn with perfect code given to them everytime If you ever get 'sloppy' code from someone, it will usually work if you can figure out the error it makes, there are only a certain number of errors and they are all documented, google it. Quote Link to comment Share on other sites More sharing options...
Bigdogcms Posted April 1, 2008 Share Posted April 1, 2008 thats how i learned php, i'ms till kinda a noobie but i've developed a shit load of apps, i created a cms, but uniflare, i'm having a problem with an app i'm creating right now do you know anything about js/php togeather? i need to echo a javascript in line function can you please help? Quote Link to comment Share on other sites More sharing options...
uniflare Posted April 1, 2008 Share Posted April 1, 2008 i have recieved your PM Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.