1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Allowing only registered members to use contact form

Discussion in 'PHP' started by tnd1000, May 1, 2012.

  1. #1
    Hello. I made a contact form for my site, and now I would like to know what sort of code to add so that only my registered members can use it.

    Any suggestions would be greatly appreciated!
     
    tnd1000, May 1, 2012 IP
  2. downloadphpscripts

    downloadphpscripts Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #2
    Hi there. You'll need to have the following (or something similar to make it work with a registered members area:

    1. You'll need some kind of php login script or authentication system that you can use to determine if the user is logged in.

    2. When your members go to the contact form, you will need to detect whether they are logged in or not using the system in step 1 above.

    3. Now you have a couple choices. You can completely forbid them from seeing the page and forward them off somewhere else using a header location or similar. Or, you can write something into the script that processes the contact form to not send if they are not logged in. For example:

    PHP:
    1.  
    2. $is_loggedin = checkuserauth(); // Or however you determine if they are logged in.
    3. if($is_loggedin !== true)
    4. {
    5. header("Location: /whereyouwantthemtogo.html");
    6. }
    7. // Process contact request here if they are logged in.
    8.  
    I hope that helps.
     
    downloadphpscripts, May 1, 2012 IP
  3. tnd1000

    tnd1000 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you very much for the prompt reply! I had an idea that that was what I needed to do, but the problem is that I have no idea where to start, as I'm somewhat new to PHP. I am using social networking software to build my site, but I had to make this form completely from scratch.

    Here's the code:

    Code (Text):
    1.  
    2. <?php
    3. function spamcheck($field)
    4.   {
    5.   //filter_var() sanitizes the e-mail
    6.   //address using FILTER_SANITIZE_EMAIL
    7.   $field=filter_var($field, FILTER_SANITIZE_EMAIL);
    8.   //filter_var() validates the e-mail
    9.   //address using FILTER_VALIDATE_EMAIL
    10.   if(filter_var($field, FILTER_VALIDATE_EMAIL))
    11.         {
    12.         return TRUE;
    13.         }
    14.   else
    15.         {
    16.         return FALSE;
    17.         }
    18.   }
    19. if (isset($_REQUEST['email']))
    20.   {//if "email" is filled out, proceed
    21.   //check if the email address is invalid
    22.   $mailcheck = spamcheck($_REQUEST['email']);
    23.   if ($mailcheck==FALSE)
    24.         {
    25.         echo "Invalid input";
    26.         }
    27.   else
    28.         {//send email
    29.         $email = $_POST['email'] ;
    30.         $eventname = $_POST['eventname'] ;
    31.         $startdate = $_POST['startdate'] ;
    32.         $enddate = $_POST['enddate'] ;
    33.         $location = $_POST['location'] ;
    34.         $details = $_POST['details'] ;
    35.        
    36.         $totalmessage = "
    37.         Event Name:    $eventname  \n
    38.         Starts:        $startdate  \n
    39.         Ends:        $enddate  \n
    40.         Location:    $location  \n
    41.         Details:    $details  \n";
    42.        
    43.         mail("someone@example.com", "Event Name: $eventname",
    44.         $totalmessage, "From: $email" );
    45.         echo "Thank you for submitting your event";
    46.         }
    47.   }
    48. else
    49.   { //if "email" is not filled out, display the form
    50. ?>
    51.   <form method="post" action="mailform.php">
    52.   Email: <input name="email" type="text" /><br />
    53.   Event Name: <input name="eventname" type="text" /><br />
    54.   Start Date: <input name="startdate" type="text" /><br />
    55.   End Date: <input name="enddate" type="text" /><br />
    56.   Location: <input name="location" type="text" /><br />
    57.   Details:<br />
    58.   <textarea name="details" rows="15" cols="40">
    59.   </textarea><br />
    60.   <input type="submit" name="submit" />
    61.   </form>
    62. <?php
    63. }
    64. ?>
    65.  
    If you could just point me in the right direction of what sort of authentication methods should be used, I'm sure I could figure the rest of it out.
     
    tnd1000, May 1, 2012 IP
  4. downloadphpscripts

    downloadphpscripts Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #4
    It really depends on how much time and effort you want to put in it. You could use something so simple as a session variable to check:

    PHP:
    1.  
    2. if($_SESSION["loggedin"] === "loggedin")
    3. {
    4. $loggedin = true;
    5. }
    6. else
    7. {
    8. $loggedin = false;
    9. }
    10.  
    11. // You'd put that at the very top of the code and then change the line where you check for the email post like so:
    12.  
    13. if (isset($_REQUEST['email']) AND $loggedin === true)
    14. // rest of code here
    15.  
    Of course you'd need a log in page where the session was actually set:

    PHP:
    1.  
    2. if($_POST["username"] === "USERNAME" AND $_POST["password"] === "PASSWORD")
    3. {
    4. $_SESSION["loggedin"] = "loggedin";
    5. }
    6.  
    You could also use cookies or other methods to authenticate the user, but that's the general idea.
     
    downloadphpscripts, May 1, 2012 IP
    tnd1000 likes this.
  5. tnd1000

    tnd1000 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Alrighty. Thank you very much, I'll definitely try that.
     
    tnd1000, May 1, 2012 IP
  6. Northize

    Northize Greenhorn

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    Wouldn't it be good if someone that isn't a member really wants to use the contact form actually could use it without signing up?
     
    Northize, May 1, 2012 IP
  7. tnd1000

    tnd1000 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Northize: That is a very good point, and I do have a general contact form for anyone to use. This form that I'm building is for submitting specific content to the site.
     
    tnd1000, May 3, 2012 IP
  8. Spudster

    Spudster Peon

    Messages:
    96
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I use this

    if ($_SESSION['username'] == true)
    include ("yourform");
    // if the user is not logged in
    else
    die ("You Must Be Loggedin To You This Feature");

    Im in a hurry since im at work and i also have school, tell me if it works or not if it doesent i will fix it up as soon as i get home.
     
    Spudster, May 7, 2012 IP
  9. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Do you have a usersystem or membership system pre-built on your website? If so I would recommend taking a look at how it authenticates users. If not I'd take a look around online at "PHP Usersystem Tutorials" theres thousands available just pick one, then have a look how they authenticate their users.
     
    PK-Host, May 7, 2012 IP
  10. tnd1000

    tnd1000 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you, Spudster, I'll definitely try that and let you know how it works! PK-Host: Yes, the software I'm using does have a built-in usersystem. I'll still take a look at those tutorials, though, just to better familiarize myself with the matter. Thank you!

    EDIT: Spudster, the code didn't work, but I think it's only because I need to find the correct way to integrate it into my software. I'll keep experimenting with it.
     
    Last edited: May 8, 2012
    tnd1000, May 8, 2012 IP
  11. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #11
    What software are you using? or is it custom made?
     
    PK-Host, May 8, 2012 IP
  12. tnd1000

    tnd1000 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It's called SocialEngine, and it's made for building social networks.
     
    tnd1000, May 8, 2012 IP
  13. adhp123

    adhp123 Guest

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thank you very much
     
    adhp123, May 8, 2012 IP
  14. Kinkypeepz

    Kinkypeepz Greenhorn

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    16
    #14
    nice code, I can use that
     
    Kinkypeepz, May 16, 2012 IP
  15. Kinkypeepz

    Kinkypeepz Greenhorn

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    16
    #15
    seems to be problem with code
     
    Kinkypeepz, May 16, 2012 IP