dolcezza Posted December 2, 2007 Share Posted December 2, 2007 All my php code is working fine, but the html after all the php isn't showing up if the if statement isn't true. Shouldn't anything after the php code is closed not be affected? I searched for an answer to this, but I wasn't able to find anything to help me understand this. Any help would be appreciated. Thanks. page is https://www.caregivingsocal.com/php/login.php If you get the error message (wrong password) There is no footer. (PS The html site is not done by me, sorry for the horrible template code) <? if (isset($_SESSION['username']) && isset($_SESSION['password'])) { header("Location: https://www.caregivingsocal.com/members.php"); } if(isset($_POST['submit'])) { if(!$_POST['username']) die("Error: You must enter your username to log in."); if(!$_POST['password']) die("Error: You must enter a password to log in."); //set cookie if checked if(!empty($_POST['stay_in'])) { $joined =''.$_POST['username'].'[]'.md5($_POST['password']).''; setcookie('login_cookie', $joined, 2147483647, '/','www.caregivingsocal.com'); } // end if //verify user $get_user = mysql_query("SELECT * FROM mem WHERE username = '".$_POST['username']."' AND user_password = '".md5($_POST['password'])."'"); $q = mysql_fetch_object($get_user); if(!$q) die("Login Failure: Please verify your username and password are correct." . mysql_error()); // set session variables $_SESSION['logged_in'] = 1; $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; session_write_close(); Header("Location: https://www.caregivingsocal.com/signin.php"); } else { //show login form ?> <form name="login" method="post" action="<?$_SERVER['PHP_SELF']; ?>"> <table> <tr><td>Username:</td><td><input type="text" id="username" name="username"></td></tr> <tr><td>Password:</td><td><input type="password" id="password" name="password"></td></tr> <tr><td>Submit: <input type="submit" value="submit" name="submit" id="submit"></td></tr> <tr><td><input type="checkbox" name="stay_in[]" checked="yes">Remember Me</td></tr></table></form> <? } // end else ?></div> </p></th> </tr> <tr> <th scope="col"><p align="right"> </p> <p align="center"> </p> <p><img src="footer.png" width="498" height="34" /></p> <p> </p> <p> </p> <pre><span class="style3">©2007 Caregiving with Love Website Design <a href="http://www.kineticsolutionsservices.com">Kinetic Solutions</a></span> </pre></th> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 2, 2007 Share Posted December 2, 2007 Let's always use long tags '<?php', not '<?' Also, you are missing an echo in your form method. <?php if (isset($_SESSION['username']) && isset($_SESSION['password'])) { header("Location: https://www.caregivingsocal.com/members.php"); } if(isset($_POST['submit'])) { if(!$_POST['username']) die("Error: You must enter your username to log in."); if(!$_POST['password']) die("Error: You must enter a password to log in."); //set cookie if checked if(!empty($_POST['stay_in'])) { $joined =''.$_POST['username'].'[]'.md5($_POST['password']).''; setcookie('login_cookie', $joined, 2147483647, '/','www.caregivingsocal.com'); } // end if //verify user $get_user = mysql_query("SELECT * FROM mem WHERE username = '".$_POST['username']."' AND user_password = '".md5($_POST['password'])."'"); $q = mysql_fetch_object($get_user); if(!$q) die("Login Failure: Please verify your username and password are correct." . mysql_error()); // set session variables $_SESSION['logged_in'] = 1; $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; session_write_close(); Header("Location: https://www.caregivingsocal.com/signin.php"); } else { //show login form ?> <form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr><td>Username:</td><td><input type="text" id="username" name="username"></td></tr> <tr><td>Password:</td><td><input type="password" id="password" name="password"></td></tr> <tr><td>Submit: <input type="submit" value="submit" name="submit" id="submit"></td></tr> <tr><td><input type="checkbox" name="stay_in[]" checked="yes">Remember Me</td></tr></table></form> <?php } // end else ?></div> </p></th> </tr> <tr> <th scope="col"><p align="right"> </p> <p align="center"> </p> <p><img src="footer.png" width="498" height="34" /></p> <p> </p> <p> </p> <pre><span class="style3">©2007 Caregiving with Love Website Design <a href="http://www.kineticsolutionsservices.com">Kinetic Solutions</a></span> </pre></th> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </body> </html> See if that gets us any love... PhREEEk Quote Link to comment Share on other sites More sharing options...
dolcezza Posted December 2, 2007 Author Share Posted December 2, 2007 Now I get: Parse error: syntax error, unexpected T_STRING in /home/care/public_html/signin.php on line 2 line 2 being ob_start(); In trying to read up on this, I am confused as to if I am doing this right... If you have a form, and you want the page to display around it, should you not use die()? if(!$_POST['username']) die("Error: You must enter your username to log in."); if(!$_POST['password']) die("Error: You must enter a password to log in."); Basically I want the error message to be between the heder an footer, is die stopping all code within/ and out of php? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 2, 2007 Share Posted December 2, 2007 die() exits the script pronto. That's it, it's over, the fat lady has sung... die() should be used if you suspect a hack attempt via an incoming variable that is way out of the expected range, or if there is a critical MySQL error. Your error message should be stored in a variable ($errorMsg or whatever), and then use whatever logic necessary that fits your script to display the error to the user. PhREEEk Quote Link to comment Share on other sites More sharing options...
dolcezza Posted December 2, 2007 Author Share Posted December 2, 2007 It exits the php script I knew, but also exits the html that comes after the closing tags? If the username and password are incorrect, you want it to die, or is there another command I should be using here? In all the books and tutorials I see them use die() if a login is wrong. Are you saying forget die and just dispaly the message? This is my first programming language other than html, and I am having a little trouble here and there understanding. Thanks for your help! Quote Link to comment Share on other sites More sharing options...
rameshfaj Posted December 2, 2007 Share Posted December 2, 2007 Missing of <html> tag is also great mistake.I didnot find that tag in the given code.Plz check also for that. Quote Link to comment Share on other sites More sharing options...
dolcezza Posted December 2, 2007 Author Share Posted December 2, 2007 It's above all the code, the menu etc... is all above it. Quote Link to comment Share on other sites More sharing options...
monkeybidz Posted December 2, 2007 Share Posted December 2, 2007 You show closing </div>, but not opening <div> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 2, 2007 Share Posted December 2, 2007 die() kills everything. no more output, not even HTML. It's not a command in particular you are missing, what you are missing is logical flow. That's why we have IF branches in code... IF things are cool, move on. IF they are not, take another direction. This is all logic, and it is up to you to keep track. PHP is the movie, your variables are the actors, and you are the director. If you allow your actors to improvise, you'll get unexpected results. It seems you might have a script that you did not write, or that you pieced together borrowing code from here or there. It does have some complicated logic to it, which is why I concluded that... but it seems you want to modify it to behave differently, which is, of course, very difficult if you didn't author it and you are new to programming. Well, there is no golden solution here. It would take you awhile to gain enough programming knowledge to take control of this script. So I guess you are left with asking people in this forum to help you make specific changes. Take them one at a time... be very descriptive on what you expect it to do. "It doesn't work" doesn't work. The folks around here will help you get those changes implemented if you're patient. You'll learn along the way as well. PhREEEk Quote Link to comment Share on other sites More sharing options...
dolcezza Posted December 2, 2007 Author Share Posted December 2, 2007 Thanks for the info... you are partially right. I read tutorials and books, then followed a similar tutorial to what I wanted, and modified it as I went. This is the first script I have strayed from the tutorial with, and mostly, I did ok. Everything works, I just want to show the footer of the html page after my login errors. From my understanding... die doesn't work because it ends all code echo doesn't work because then it continues and lets the user go to protected pages I saw a post in a forum that said die(put the neat ending in here); which didn't work. So how do you stop the script, but still put more html after it? Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 2, 2007 Share Posted December 2, 2007 Totally agree with PHP_PhREEEk Now check this your formatted code... hope you find the changes <?php session_start(); # check if user name or password is set if (isset($_SESSION['username']) && isset($_SESSION['password'])) { # redirects to members pages..... header("Location: https://www.caregivingsocal.com/members.php"); } # if submit button is clicked... if(isset($_POST['submit'])) { if(!$_POST['username'] || !$_POST['password']) echo "Error: You must enter a password to log in."; //set cookie if checked if(!empty($_POST['stay_in'])) { $joined =''.$_POST['username'].'[]'.md5($_POST['password']).''; setcookie('login_cookie', $joined, 2147483647, '/','www.caregivingsocal.com'); } // end if //verify user $get_user = mysql_query("SELECT * FROM mem WHERE username = '".$_POST['username']."' AND user_password = '".md5($_POST['password'])."'"); $q = mysql_fetch_object($get_user); if(!$q) die("Login Failure: Please verify your username and password are correct." . mysql_error()); // set session variables $_SESSION['logged_in'] = 1; $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; session_write_close(); header ("Location: https://www.caregivingsocal.com/signin.php"); } else { //show login form ?> <div> <form name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr><td>Username:</td><td><input type="text" id="username" name="username"></td></tr> <tr><td>Password:</td><td><input type="password" id="password" name="password"></td></tr> <tr><td>Submit: <input type="submit" value="submit" name="submit" id="submit"></td></tr> <tr><td><input type="checkbox" name="stay_in[]" checked="yes">Remember Me</td></tr></table></form> <?php } // end else ?> </div> <tr> <th scope="col"><p align="right"> </p> <p align="center"> </p> <p><img src="footer.png" width="498" height="34" /></p> <p> </p> <p> </p> <pre><span class="style3">©2007 Caregiving with Love Website Design <a href="http://www.kineticsolutionsservices.com">Kinetic Solutions</a></span> </pre></th> </tr> </table> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </body> </html> 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.