SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Chicago
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I need the users to enter their email prior to visiting a website

    I'm trying to find out how could I setup a page where a user needs to enter his email address prior to entering a photo gallery for example. I also need those emails recorded either in a database or in a text file that I can use later on for mail mass. If somebody could point out some php code and maybe MySQL I would really appreciate it

    Thanks

  2. #2
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you allowing users to opt out? If not, that's bad nettiquette and I for one would not put my email address into a web form unless there was an option to not receive spam.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Chicago
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by d11wtq View Post
    Are you allowing users to opt out? If not, that's bad nettiquette and I for one would not put my email address into a web form unless there was an option to not receive spam.
    95% of the people are already part of the forum, but I think it would be wise to put an opt out option.

  4. #4
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya, you haven't really given enough info so I will just guess Do the people who use the forum have to login? If so, then you must be storing their details in a database already. If you don't have a field for email address then add one to the user table. If you are using sessions to log users in add the email address to a session as well. When they go to the gallery page, you can check if they are in a valid session or not to decide if they can enter. As far as mass emailing goes, that is easy as you can query the db to grab all the addresses or depending on the info held on users you could select certain groups, e.g. everyone between 18-25 years old.
    Hope this helps, Tina

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Chicago
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey Tina,

    ok, to be more specific, I'm not going to use their login address from either the forum or the photo page, I would rather setup a page just with an email box, and after they enter their email, then they could view the photo page.
    So, I'm assuming I could create a table in the database that holds only the emails and I can query that.
    I just need to figure out all this PHP code that I would need to accomplish this. Another issue will be what if a user comes back and he enters his email address again, should I query the database and drop the duplicates or I should just somehow verify before if the email address exists and it will not be recorded.

  6. #6
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, please bear in mind that I have just written this in the last 5 minutes and I have not tested it It is not very secure and does not check if the user does not enter an email address or an invalid email address. I am sure you can do that though

    Database table
    Code:
    CREATE TABLE `users` (
      `ID` int(11) NOT NULL auto_increment,
      `email` varchar(60) NOT NULL,
      KEY `ID` (`ID`)
    ) ENGINE=MyISAM;
    login.php
    PHP Code:
    <?php
    include('dbconnect.php'); // connect to the database

    if(isset($submit)) { // if user presses submit
          
    $email $_POST['email']; // grab posted email value
        
    $sql mysql_query("SELECT email FROM users WHERE email = '$email'"); // query database
        
    $num_rows mysql_num_rows($sql); // grab number of rows
        
        
    if($num_rows <= 0) { 
            
    mysql_query("INSERT INTO users (email) VALUES ('$email')"); // add email address to database
            
    session_start(); // start session
            
    $_SESSION['email'] = $email// add email address to session
            
    header('Location: gallery.php'); // send user to gallery page
        
    } elseif($num_rows 0) {
              
    session_start();
            
    $_SESSION['email'] = $email;
            
    header('Location: gallery.php');
        }
    }
    ?>

    <html>
    <head>
    <title>Photo Login Page</title>
    </head>

    <body>
    <form action="<?php echo $PHP_SELF?>" method="post">
    <input type="text" name="email" size="50" />
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
    </html>
    Add the following to the top of the gallery page and any other page that you want the user to have to use their email address to view
    PHP Code:
    <?php
        header
    ("Expires: Tue, 15 May 2001 10:00:01 GMT");    // Date in the past
        
    header ("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT"); // always modified
        
    header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
        
    header ("Pragma: no-cache");                          // HTTP/1.0
        
        
    session_start();

        if(!isset(
    $_SESSION['email'])) { // if there is no session
               
    header('Location: login.php'); // send user to login page
               
    exit();
        }
    ?>
    Hope this helps you

  7. #7
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    /me ponders if that's Tina in the avatar

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Chicago
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    awesome, thank you Tina. One step closer.

    do I also need to create a dbconnect.php file?

    So far, here is what I did: I made a login_email.php file with the first PHP code you gave me, then secondly, i added the 2nd code to the index.php file, changed the path to point to index.php instead of gallery.php but now I get a dbconnect error. I also ran the query in the DB, so the table was created. Am I missing something?

  9. #9
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,101
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    dbconnect.php typically has the server name (localhost) the username and password and then the database you want to connect to.

    this is what I typically use
    Code:
    // Connect to the database server
      $dbcnx = mysql_connect('localhost', 'root', 'poi');
      if (!$dbcnx) {
        echo( "<p>Unable to connect to the " .
              "database server at this time.</p>" );
        exit();
      }
      // Select the  database
      if (! mysql_select_db("maillist") ) {    
    echo( "<p>Unable to locate the mail list " .
              "database at this time.</p>" );
        exit();  }
    // this is helpful in preventing sql injection and I dont trust magicquotes
    function sanitize_me($arr) 
    {
        foreach ($arr as $k => $v) 
        { 
            if (is_array($v)) 
                { 
    			$arr[$k] = preg_replace("[\x5c\]","",$v);
    //			$arr[$k] = strip_magic_quotes($v); // I like x5c better
    			$arr[$k] = htmlentities("$arr[$k]",ENT_QUOTES);
    			$arr[$k] = mysql_real_escape_string($arr[$k]); 
    			} 
            else 
                { 
    			$arr[$k] = preg_replace("[\x5c\]","",$v); //this removes all \s , also magic quotes is turned off but why allow \s from $GET.
    //			$arr[$k] = stripslashes($v); // I like x5c better
    			$arr[$k] = htmlentities("$arr[$k]",ENT_QUOTES); // convert the special Chars.
    			$arr[$k] = mysql_real_escape_string($arr[$k]);  // this should never happen but just in case.
    			 } 
        } 
        return $arr; 
    } 
        if (!empty($_GET))    { $_GET =  sanitize_me($_GET);    } 
        if (!empty($_POST))   { $_POST =  sanitize_me($_POST);   } 
        if (!empty($_COOKIE)) { $_COOKIE = sanitize_me($_COOKIE); }
    HTH

    Cheers
    Loren

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Chicago
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you lorenw, I will create a dbconnect.php file and use your code, of course with my database information. My config file that has all the database info is called config.inc.php but it definitely has less code, basically just the login name and the table prefix.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •