jmr3460 Posted April 18, 2009 Share Posted April 18, 2009 Can I run an if else statement to check and see if I got into my db table? if (database_connect()) { echo "I got in." } else { echo "I did not get in." }; Can someone help me with this? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 18, 2009 Share Posted April 18, 2009 Does database_connect() return a value? Quote Link to comment Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 It is mysql_connect actually I think Quote Link to comment Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 I just inserted this code into my script and it echoed "i got in" //connect to server and select database $host = 'localhost'; $username = 'simplic5_jmr3460'; $password = 'freedom98714'; $database ='simplic5_users'; $mysql = mysql_connect($host, $username, $password); mysql_select_db($database); //$mysqli = mysql_connect("$host", "$username","$password","simplic5_users"); if ($mysql){ echo "I got in."; } else { echo "Not a chance."; }; Does this mean I got in? Quote Link to comment Share on other sites More sharing options...
seaweed Posted April 18, 2009 Share Posted April 18, 2009 No, I think it just means $mysql isn't NULL, and you assigned it up above. If you don't get in you'll get an error, if you add error to the end of the command, it will display an error if the connection fails, like this: $mysql = mysql_connect($host, $username, $password) or die ('Error connecting to database'); mysql_select_db($database); Quote Link to comment Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 Yes. It means you got in. http://php.net/mysql_connect It clearly states it returns FALSE if it fails to connect. Quote Link to comment Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 I tried the or die and it still tells me I got in. So if I got in then why can't I retrieve anything from my user database. <?php //check for required fields from the form if ((!isset($_POST["username"])) || (!isset($_POST["password"]))) { header("Location: index.html"); exit; } //connect to server and select database $host = 'localhost'; $username = 'simplic5_jmr3460'; $password = 'freedom98714'; $database ='simplic5_users'; $mysql = mysql_connect($host, $username, $password, $database); mysql_select_db($database); //$mysqli = mysql_connect("$host", "$username","$password","simplic5_users"); //create and issue the query $sql = "SELECT f_name, l_name FROM auth_users WHERE username = '".$_POST["username"]."' AND password = PASSWORD('".$_POST["password"]."')"; $result = mysql_query($sql) or die(mysql_error($mysql, $sql, $result)); //get the number of rows in the result set; should be 1 if a match if (mysql_num_rows($result) == 1) { //if authorized, get the values of f_name l_name while ($info = mysql_fetch_array($result)) { $f_name = stripslashes($info['f_name']); $l_name = stripslashes($info['l_name']); } //set authorization cookie setcookie("auth", "1", 0, "/", "localhost", 0); //create display string $display_block = " <p>".$f_name." ".$l_name." is authorized!</p> <p>Authorized Users' Menu:</p> <ul> <li><a href=\"secretpage.php\">secret page</a></li> </ul>"; } else { //redirect back to login form if not authorized header("Location: index.html"); exit; } ?> <html> <head> <title>User Login</title> </head> <body> <?php echo "$display_block"; ?> </body> </html> When I open this file It goes to the redirect after the else statement. Quote Link to comment Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 I suggest removing your db username and password from that code snippet. Quote Link to comment Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 I plan to use an include() once I have the script working. I am trying to get a password protect working for an admin page I am working on. I could not get the link to work. Quote Link to comment Share on other sites More sharing options...
keeB Posted April 18, 2009 Share Posted April 18, 2009 No, I mean -- I now know the username and password to your database. This is not a good thing. Quote Link to comment Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 This database is on my test site, it is only setup for the purposde to get the password protect working. How do I change if it does not have a modify link in the window? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 18, 2009 Share Posted April 18, 2009 I'd recommend putting or die(mysql_error()) after every query and connection. As always. Quote Link to comment Share on other sites More sharing options...
jmr3460 Posted April 18, 2009 Author Share Posted April 18, 2009 The script seems to not be setting the cookie. Can someone help with this? The script has the cookie as follows: setcookie("auth", "1", time()=3600, "/", "whatever.com", 0); When I check for this cookie not in the list. I have changed whatever.com with my domain. I understand the setcookie(name of cookie, value of cookie, expiration time,directory cookie is set for, domain cookie is set for, security setting) Why is this cookie not setting? Quote Link to comment Share on other sites More sharing options...
soak Posted April 18, 2009 Share Posted April 18, 2009 Your expiry time is incorrect, assuming you want the cookie to last one hour your string should be: setcookie("auth", "1", time()+3600, "/", "whatever.com", 0); 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.