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:

    
    $is_loggedin = checkuserauth(); // Or however you determine if they are logged in.
    if($is_loggedin !== true)
    {
    header("Location: /whereyouwantthemtogo.html");
    }
    // Process contact request here if they are logged in.
    
    PHP:
    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:

    
    <?php
    function spamcheck($field)
      {
      //filter_var() sanitizes the e-mail
      //address using FILTER_SANITIZE_EMAIL
      $field=filter_var($field, FILTER_SANITIZE_EMAIL);
      //filter_var() validates the e-mail
      //address using FILTER_VALIDATE_EMAIL
      if(filter_var($field, FILTER_VALIDATE_EMAIL))
            {
            return TRUE;
            }
      else
            {
            return FALSE;
            }
      }
    if (isset($_REQUEST['email']))
      {//if "email" is filled out, proceed
      //check if the email address is invalid
      $mailcheck = spamcheck($_REQUEST['email']);
      if ($mailcheck==FALSE)
            {
            echo "Invalid input";
            }
      else
            {//send email
            $email = $_POST['email'] ;
            $eventname = $_POST['eventname'] ;
            $startdate = $_POST['startdate'] ;
            $enddate = $_POST['enddate'] ;
            $location = $_POST['location'] ;
            $details = $_POST['details'] ;
            
            $totalmessage = "
            Event Name:    $eventname  \n
            Starts:        $startdate  \n
            Ends:        $enddate  \n
            Location:    $location  \n
            Details:    $details  \n";
            
            mail("someone@example.com", "Event Name: $eventname",
            $totalmessage, "From: $email" );
            echo "Thank you for submitting your event";
            }
      }
    else
      { //if "email" is not filled out, display the form
    ?>
      <form method="post" action="mailform.php">
      Email: <input name="email" type="text" /><br />
      Event Name: <input name="eventname" type="text" /><br />
      Start Date: <input name="startdate" type="text" /><br />
      End Date: <input name="enddate" type="text" /><br />
      Location: <input name="location" type="text" /><br />
      Details:<br />
      <textarea name="details" rows="15" cols="40">
      </textarea><br />
      <input type="submit" name="submit" />
      </form>
    <?php
    }
    ?>
    
    Code (markup):
    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:

    
    session_start();
    if($_SESSION["loggedin"] === "loggedin")
    {
    $loggedin = true;
    }
    else
    {
    $loggedin = false;
    }
    
    // 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:
    
    if (isset($_REQUEST['email']) AND $loggedin === true)
    // rest of code here
    
    PHP:
    Of course you'd need a log in page where the session was actually set:

    
    session_start();
    if($_POST["username"] === "USERNAME" AND $_POST["password"] === "PASSWORD")
    {
    $_SESSION["loggedin"] = "loggedin";
    }
    
    PHP:
    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