Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/158287-multiple-if-statement-conditions-help/
Share on other sites

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

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.