carleihar Posted January 17, 2010 Share Posted January 17, 2010 I've somewhat figured out my cookie login problem. I feel like I'm so close but now I'm stuck. I've been looking at this for hours. <?php // Make the connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //(this is actually filled out) if(isset($_COOKIE['username']) || $_COOKIE['password']){ $username = $_COOKIE['username']; $password = $_COOKIE['password']; if (isset($_COOKIE['username']) && ($_COOKIE['password'])){ echo 'cookie logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; } } if(isset($_POST['submit'])){ //if the form has been submitted $username = $_POST['username']; $password = $_POST['password']; if ($result = mysqli_query($dbc, "SELECT COUNT(*) FROM users WHERE username='" . $username . "' AND password='" . $password . "'")) { $row = mysqli_fetch_assoc($result); if ($row['COUNT(*)'] == 1) { setcookie("username", $username, time()+3600, "/"); setcookie("password", $password, time()+3600, "/"); echo 'logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; ?> <script language="javascript"> window.location = "http://www.liveequian.com/htdocs/pages/login_main.php"; </script> <?php } else { //the username does not match echo 'Log in failed'; } } else { echo 'something failed. who knows.'; } } else { //the form was not submitted if(!isset($_COOKIE['username'])){ echo '<form action="" method="POST">'; echo 'Username - <input name="username" type="text"><br />'; echo 'Password - <input name="password" type="password"><br />'; echo '<input name="submit" type="submit" value="Submit"><br />'; echo '</form>'; } else { echo 'You are already logged in!'; } } ?> It comes out with: "something failed. who knows." Alternatively, this code comes up with "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/content/e/q/u/equianadmin/html/htdocs/pages/loginhelp.php on line 33 Log in failed" <?php // Set the database access information as constants: DEFINE ('DB_USER', 'liveequianusers'); DEFINE ('DB_PASSWORD', 'Salliell9.'); DEFINE ('DB_HOST', 'liveequianusers.db.5225436.hostedresource.com'); DEFINE ('DB_NAME', 'liveequianusers'); // Make the connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if(isset($_COOKIE['username']) || $_COOKIE['password']){ $username = $_COOKIE['username']; $password = $_COOKIE['password']; if (isset($_COOKIE['username']) && ($_COOKIE['password'])){ echo 'cookie logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; } } if(isset($_POST['submit'])){ //if the form has been submitted $username = $_POST['username']; $password = $_POST['password']; $q = "SELECT username FROM users WHERE (username='$username' AND password=SHA1('$password')"; $r = mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { // A match was made. setcookie("username", $username, time()+3600, "/"); setcookie("password", $password, time()+3600, "/"); echo 'logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; ?> <script language="javascript"> window.location = "http://www.liveequian.com/htdocs/pages/login_main.php"; </script> <?php } else { //the username does not match echo 'Log in failed'; } } else { //the form was not submitted if(!isset($_COOKIE['username'])){ echo '<form action="" method="POST">'; echo 'Username - <input name="username" type="text"><br />'; echo 'Password - <input name="password" type="password"><br />'; echo '<input name="submit" type="submit" value="Submit"><br />'; echo '</form>'; } else { echo 'You are already logged in!'; } } ?> Any help? Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/ Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 You need to debug your mysqli queries.. for you first code put mysqli_error(); in the else statment that is thowing the "something failed. who knows" or for the second one $r = mysqli_query ($dbc, $q) or trigger_error(mysqli_error()); Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996343 Share on other sites More sharing options...
carleihar Posted January 17, 2010 Author Share Posted January 17, 2010 The first one: Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/content/e/q/u/equianadmin/html/htdocs/pages/loginhelp.php on line 58 something failed. who knows. Line 58 is where i put the mysqli_error(); The second one: Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/content/e/q/u/equianadmin/html/htdocs/pages/loginhelp.php on line 31 Notice: in /home/content/e/q/u/equianadmin/html/htdocs/pages/loginhelp.php on line 31 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/content/e/q/u/equianadmin/html/htdocs/pages/loginhelp.php on line 33 Log in failed Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996344 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Sorry, forgot you need to pass your link to mysqli_error so it will be mysqli_error($your_link_var) Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996346 Share on other sites More sharing options...
carleihar Posted January 17, 2010 Author Share Posted January 17, 2010 I changed the first one to include the link, and it didn't say anything new. It just said "something failed..". Thank you for helping me, by the way! Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996351 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Can you show us the code youve used. Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996352 Share on other sites More sharing options...
carleihar Posted January 17, 2010 Author Share Posted January 17, 2010 <?php // Set the database access information as constants: DEFINE ('DB_USER', 'filed'); DEFINE ('DB_PASSWORD', 'out.'); DEFINE ('DB_HOST', 'respectively'); DEFINE ('DB_NAME', ''); // Make the connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //(this is actually filled out) if(isset($_COOKIE['username']) || $_COOKIE['password']){ $username = $_COOKIE['username']; $password = $_COOKIE['password']; if (isset($_COOKIE['username']) && ($_COOKIE['password'])){ echo 'cookie logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; } } if(isset($_POST['submit'])){ //if the form has been submitted $username = $_POST['username']; $password = $_POST['password']; if ($result = mysqli_query($dbc, "SELECT COUNT(*) FROM users WHERE username='" . $username . "' AND password='" . $password . "'")) { $row = mysqli_fetch_assoc($result); if ($row['COUNT(*)'] == 1) { setcookie("username", $username, time()+3600, "/"); setcookie("password", $password, time()+3600, "/"); echo 'logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; ?> <script language="javascript"> window.location = "http://www.liveequian.com/htdocs/pages/login_main.php"; </script> <?php } else { //the username does not match echo 'Log in failed'; } } else { mysqli_error($dbc); echo 'something failed. who knows.'; } } else { //the form was not submitted if(!isset($_COOKIE['username'])){ echo '<form action="" method="POST">'; echo 'Username - <input name="username" type="text"><br />'; echo 'Password - <input name="password" type="password"><br />'; echo '<input name="submit" type="submit" value="Submit"><br />'; echo '</form>'; } else { echo 'You are already logged in!'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996356 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 You need to echo the mysqli_error.. echo 'something failed. because ' . mysqli_error($dbc); Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996359 Share on other sites More sharing options...
carleihar Posted January 17, 2010 Author Share Posted January 17, 2010 Ahh. Duh. Sorry, I haven't dealt too much with mysqli_error. I guess i should be.. So, I figured out what was giving me the original error. Now it's giving me this: "something failed. because You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1something failed. who knows." <?php // Set the database access information as constants: // Make the connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); //(this is actually filled out) if(isset($_COOKIE['username']) || $_COOKIE['password']){ $username = $_COOKIE['username']; $password = $_COOKIE['password']; if (isset($_COOKIE['username']) && ($_COOKIE['password'])){ echo 'cookie logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; } } if(isset($_POST['submit'])){ //if the form has been submitted $username = $_POST['username']; $password = $_POST['password']; if ($result = mysqli_query($dbc, "SELECT COUNT(*) FROM users WHERE username='" . $username . "' AND pass=SHA1('$password'))")) { $row = mysqli_fetch_assoc($result); if ($row['COUNT(*)'] == 1) { setcookie("username", $username, time()+3600, "/"); setcookie("password", $password, time()+3600, "/"); echo 'logged in<br />'; echo 'Click <a href="logout.php">here</a> to log out'; ?> <script language="javascript"> window.location = "http://www.liveequian.com/htdocs/pages/login_main.php"; </script> <?php } else { //the username does not match echo 'Log in failed'; echo 'bad because ' . mysqli_error($dbc); } } else { echo 'something failed. because ' . mysqli_error($dbc); echo 'something failed. who knows.'; } } else { //the form was not submitted if(!isset($_COOKIE['username'])){ echo '<form action="" method="POST">'; echo 'Username - <input name="username" type="text"><br />'; echo 'Password - <input name="password" type="password"><br />'; echo '<input name="submit" type="submit" value="Submit"><br />'; echo '</form>'; } else { echo 'You are already logged in!'; } } ?> Again, thank you so much, you're being very helpful! Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996366 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 if ($result = mysqli_query($dbc, "SELECT COUNT(*) FROM users WHERE username='" . $username . "' AND pass=SHA1('$password'))")) { should be if ($result = mysqli_query($dbc, "SELECT COUNT(*) FROM users WHERE username='" . $username . "' AND pass=SHA1('$password')")) { Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996368 Share on other sites More sharing options...
carleihar Posted January 17, 2010 Author Share Posted January 17, 2010 YAYY! THANK YOU THANK YOU THANK YOU! Quote Link to comment https://forums.phpfreaks.com/topic/188743-new-cookie-problem/#findComment-996370 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.