toledo Posted March 10, 2012 Share Posted March 10, 2012 Hello all, I am brand new to PHP and MySQL. I have created my login page and necessary php files to check the password and authenticate. If I execute my script directly with PHP, i.e. C:\php>php.exe check_login.php, then the connection to MySQL db is successful, but when I am using my login page from my web server, then it doesn't work. (I can't even get the log where to trace and see the error being thrown). Here's what I have done: - Created MySQL table "users" and have an entry in it. - Created login.php as follow: <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="check_login.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"> </td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> - created check_login.php <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="admin"; // Mysql password $db_name="wsc"; // Database name $tbl_name="users"; // Table name // Connect to server and select database. mysql_connect($host, $username, $password)or die("cannot connect"); mysql_select_db($db_name)or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($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"; } ?> - created login_success.php <? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html> - created [b]logout.php[/b] [code] <? session_start(); session_destroy(); ?> When I enter my user name/password, then it gets redirected to check_login.php, but i see a blank screen. If it's successful, i should see "Log in Successful" message, as I have in my login_success.php. When I execute this script directly from php as follow: (C:\php\php.exe test.php) then I get success message. <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="admin"; // Mysql password $db_name="wsc"; // Database name $tbl_name="users"; // Table name // Connect to server and select database. mysql_connect($host, $username, $password)or die("cannot connect"); echo "Connected"; mysql_select_db($db_name)or die("cannot select DB"); echo "DB selected"; // username and password sent from form $myusername="wsc_user"; $mypassword="wsc"; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM " . $tbl_name . " WHERE username='" . $myusername . "' and password='" . $mypassword . "'"; echo $sql; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ echo "sucess"; session_register($myusername); session_register($mypassword); //header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> Can someone please help? Really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/ Share on other sites More sharing options...
AyKay47 Posted March 10, 2012 Share Posted March 10, 2012 Well I only glanced at the code and I already see a couple of things. It's obvious that you do not have error_reporting on. so place these two line at the top of the PHP code and fix any errors that you receive. error_reporting(-1); ini_set('display_errors', 'On'); Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1325735 Share on other sites More sharing options...
toledo Posted March 10, 2012 Author Share Posted March 10, 2012 Thanks Kay for quick reply. I did add these two lines, and here's the error I get Fatal error: Call to undefined function mysql_connect() in E:\Program Files\Apache Group\Apache2\htdocs\test\check_login.php on line 13 and line 13 in my check_login.php is the mysql_connect() statement, as: mysql_connect($host, $username, $password)or die("cannot connect"); it appears that mysql_connect() can't be found? do I need to do an include_once() to some files or something? Thanks again for help... Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1325736 Share on other sites More sharing options...
trq Posted March 10, 2012 Share Posted March 10, 2012 include won;t help, you need to make sure the extension is installed. Check your php.ini file. Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1325739 Share on other sites More sharing options...
ttocskcaj Posted March 10, 2012 Share Posted March 10, 2012 Refer to this sticky: http://www.phpfreaks.com/forums/index.php?topic=126354.0 It's about the Call to undefined function mysql_connect() error. Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1325834 Share on other sites More sharing options...
toledo Posted March 13, 2012 Author Share Posted March 13, 2012 I did follow the other notes, and I made sure all those points that were suggested, are done. But still I am getting this error. Here's what I have: php.ini - C:\php5\php.ini (is being used, I can see my changes via phpinfo() command. - extension_dir = "C:\php5\ext" (C:\php5 is my PHP installation directory) - extension=php_mysql.dll (removed simicolon) - libmysql.dll is copied to C:\WINDOWS and C:\php5\ext - C:\php5 is in my path system variable. - extension=php_mysql.dll not sure what else am I missing? Can someone please help!!!!! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1326879 Share on other sites More sharing options...
Muddy_Funster Posted March 13, 2012 Share Posted March 13, 2012 did you restart your web server after you made the changes? Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1326887 Share on other sites More sharing options...
toledo Posted March 13, 2012 Author Share Posted March 13, 2012 Yes, I did. I forgot to mention. Quote Link to comment https://forums.phpfreaks.com/topic/258624-cant-connect-to-mysql-from-my-loginphp-please-help/#findComment-1326934 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.