andrew_biggart Posted May 15, 2009 Share Posted May 15, 2009 Ok im trying to make a suggestions box for my site but i only want users that are logged in to be able to use this! and if they arent then redirect them to the loggin page when ever they try! I have got it working to an extent apart from the redirection bit. Here is the code i ama using! <?php include("config.php"); if(isset($_SESSION['myusername'])) { if(isset($_POST['add_suggestion'])){ $Suggestion_username=$_SESSION['myusername']; $Suggestion_avatar=$_SESSION['myavatar']; $Suggestion_comment=$_POST['Suggestion_comment']; $sql="INSERT INTO Admin_suggestionsT (Suggestion_username, Suggestion_comment, Suggestion_avatar)VALUES('$Suggestion_username', '$Suggestion_comment', '$Suggestion_avatar')"; $result=mysql_query($sql); if($result){ echo "<h1 class=comment_status1>Thanks for your feedback, AdMiN!</h1>"; } else { echo "<h1 class=comment_status2>Ops, try again !</h1>"; } // close connection mysql_close(); } else{ }; } else{ header("location:login.php"); } ?> <form method="post" action="#"> <table> <tr><td></td></tr> <tr><td class="sug_headers"> <input class="sug_user"name="Suggestion_username" visible type="text" style="width: 94px" value="<?php if(isset($_SESSION['myusername']) == True) { echo " ".$_SESSION["myusername"]. " ";} else {echo " Not Registered ";} ?>"></input> </td></tr> <tr><td><textarea class="comments" name="Suggestion_comment">Hear at weloveweed mansion we appreciate your feedback, so feel free to add your comments and dont fucking abuse it <3</textarea></td></tr> <tr><td class="comments" style="height: 34px"> <input class="submit_sug" name="Reset" type="reset" value="D'oh" /><input class="submit_sug" name="add_suggestion" type="submit" value="Whooohoo" /> </td></tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/ Share on other sites More sharing options...
andrew_biggart Posted May 15, 2009 Author Share Posted May 15, 2009 Its just trying to get my head round what goes where? Quote Link to comment https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/#findComment-834862 Share on other sites More sharing options...
andrew_biggart Posted May 15, 2009 Author Share Posted May 15, 2009 can anyone please help? i can get everything working apart from the if statement for the session as it doesnt redirect when the user is not logged in it just doesnt submit the data to the database! Quote Link to comment https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/#findComment-834871 Share on other sites More sharing options...
Zhadus Posted May 15, 2009 Share Posted May 15, 2009 Why do you have these lines? else{ }; Quote Link to comment https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/#findComment-834889 Share on other sites More sharing options...
Maq Posted May 15, 2009 Share Posted May 15, 2009 can anyone please help? i can get everything working apart from the if statement for the session as it doesnt redirect when the user is not logged in it just doesnt submit the data to the database! What exactly happens? Do you get any errors? Does it always INSERT even if you're not logged in? Here's your code fixed up: include("config.php"); if(isset($_POST['add_suggestion'])) { if(isset($_SESSION['myusername'])) { $Suggestion_username=$_SESSION['myusername']; $Suggestion_avatar=$_SESSION['myavatar']; $Suggestion_comment=$_POST['Suggestion_comment']; $sql="INSERT INTO Admin_suggestionsT (Suggestion_username, Suggestion_comment, Suggestion_avatar)VALUES('$Suggestion_username', '$Suggestion_comment', '$Suggestion_avatar')"; $result=mysql_query($sql); if($result) { echo "Thanks for your feedback, AdMiN!"; } else { echo "Ops, try again !"; } mysql_close(); } else { header("location:login.php"); } } ?> </pre> <form method="post" action="#"> "> Hear at weloveweed mansion we appreciate your feedback, so feel free to add your comments and dont fucking abuse it </form Quote Link to comment https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/#findComment-834898 Share on other sites More sharing options...
mikr Posted May 15, 2009 Share Posted May 15, 2009 If I understand you right, you are saying that when the user isn't logged in, it won't submit the data to the db (which is good), but it also won't redirect to a new page (which is bad). A couple possibilities. If there is a blank line above your php code in that file, the header will fail to work. You can't output any html (even a blank line) before the header, else the header will be ignored. Second, Location requires an absolute address. Some clients allow a relative one (such as you are using), but those following the standards will not. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself. Capitalization is also important. Capitalize the L. Hope all that helps. Quote Link to comment https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/#findComment-834973 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.