Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 842
  • Last Modified:

Capturing Name and Email address before allowing a webpage to be viewed.

Hello

Our marketing department have asked for a list of name and email addresses of people that view a specific webpage.

Lets say we have this page:

http://www.mysite.com/tutorials

Before a user can view the tutorials, they must enter their Name and Email Address into a form.  They can then view the tutorials.

Their Name and Email address needs to be saved in some form on the server so Marketing can view it.

Most of the current website uses HTML and PHP.  I can create a mySQL database to store the names and addresses if necessary, but would prefer a simpler way of doing it.

I hope what I am after makes sense, thanks for any advice.

Kind Regards.
0
JakeSpencer
Asked:
JakeSpencer
  • 5
  • 3
  • 3
4 Solutions
 
Lukasz ChmielewskiCommented:
This is the simplest way. What you are thinking about is storing it in a file, which is the only alternative regarding using only html and php. Storing this kind of data in a files is not comfortable neither easy to retrieve using html and php (or should I say easier than with MySQL).

Use the database, then you can create a module for your Marketing to view the data in a nice tabular way.
0
 
JakeSpencerAuthor Commented:
Thanks.  Herein lies my problem.

I can create a database and database user in phpMyadmin.  I can create a form in Dreamweaver.

I have no idea how to make the form write to the database then continue to load the tutorials page.

Appreciate any pointers.

Many Thanks
0
 
Lukasz ChmielewskiCommented:
OK, so let's start with this. Is the email and name required for login ? I mean - when the user is registered ? If not, can anybody access the tutorials only by entering the email and address ? There could be problems with dummy email addresses.

What about the registration process ? What do you think. This could authenticate the user, so he could enter the tuts AFTER logging into the form.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
JakeSpencerAuthor Commented:
Hello

At this stage no validation is required.  Just a name and Email address.

So just a quick form "Please Enter your name and email address to access the tutorials".

The user enters a name and email address, presses Continue and the tutorials page loads.  When pressing Continue the data must be stored in the database.

I realise Validation would be better, but at this stage want to keep it as simple as possible.

Thanks

0
 
Lukasz ChmielewskiCommented:
You would have to set cookie variables or set the session variables for the user that did enter name and email, so I guess this would be the case of the page with tutorials

<?php
session_start();
if(!empty($_SESSION[email]) && !empty($_SESSION[name])){
   // show the page
}
else{
   // display the form
   // and after inserting the data, store them in the database
}
?>

Open in new window

0
 
JakeSpencerAuthor Commented:
Thanks for the advice.  I'm a complete newbie to PHP and those pages look very indepth.  I do not need any form of username / authentication or password etc.

Just for a name and email address to be entered so Marketing can see who actually looks at the page.

Perhaps I will have to tell them it is beyond my abilities as these solutions look too complicated for what they want.

Mant thanks for trying to help though.
0
 
Ray PaseurCommented:
This seems to test out OK.  http://www.laprbass.com/RAY_temp_jakespencer.php
You might want to do some kind of data validation to see that the email address is routable.
<?php // RAY_temp_jakespencer.php
error_reporting(E_ALL);

// REQUIRED AT PHP 5.1+
date_default_timezone_set('America/Chicago');

// HOW LONG SHOULD AUTHORIZATION CONTINUE
define('COOKIE_LIFE', 7*60*60*24); // A WEEK IN SECONDS ( = 7 * 86,400 )

// IF THE COOKIE IS NOT SET
if (!isset($_COOKIE["auth"]))
{
    // IF THE NAME AND EMAIL ARE FILLED IN
    if ( (!empty($_POST["name"])) && (!empty($_POST["mail"])) )
    {
        // COPY THE NAME AND EMAIL INTO YOUR DATA SET OR FILE HERE
        // (MIGHT ADD SANITY CHECKS?)

        // COMPUTE COOKIE EXPIRATION
        $cookie_expires = time() + date('Z') + COOKIE_LIFE;

        // SEE http://us2.php.net/manual/en/function.setcookie.php
        setcookie("auth", "Y", $cookie_expires);
    }

    // NO COOKIE AND NO POST FIELDS?  PUT UP THE FORM
    else
    {
        $form = NULL;
        $form .= 'PLEASE FILL IN THIS FORM TO SEE THE TUTORIAL';
        $form .= '<form method="post">'         . PHP_EOL;
        $form .= 'NAME: <input name="name" />'  . PHP_EOL;
        $form .= 'EMAIL: <input name="mail" />' . PHP_EOL;
        $form .= '<input type="submit" />'      . PHP_EOL;
        $form .= '</form>'                      . PHP_EOL;

        echo $form;
        die();
    }
}

// IF WE GET HERE, WE HAVE RECEIVED THE AUTHENTICATION NAME AND EMAIL
echo "HERE IS THE TUTORIAL";

Open in new window

0
 
JakeSpencerAuthor Commented:
That looks great.  How do I give it the database name to write the name and email address to?

Many Many thanks
0
 
Ray PaseurCommented:
That's probably a very different question, and one with a lot more moving parts.  Have you got much experience in PHP and MySQL programming?  If not, this is the book to get.
http://www.sitepoint.com/books/phpmysql4/

The general strategy I would use would be to set up a connect/select script that gets included in your other scripts.  Then the data base will always be available to your scripts.  You can write and run INSERT or SELECT queries to load and retrieve information from the data model.  Here is a teaching example showing some of the code you would need. This assumes the existence of a data base and of a table with the right kind of column definitions.  It also assumes that it was started by POST that sent us a field called "username", etc.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES




// ESCAPING A DATA FIELD FOR USE IN MYSQL QUERIES
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
$safe_username = mysql_real_escape_string($_POST["username"]);




// MAKING AN INSERT QUERY AND TESTING THE RESULTS
$sql = "INSERT INTO my_table (username) VALUES ('$safe_username')";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED - PER THE DB CONNECTION
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php
$id  = mysql_insert_id($db_connection);

Open in new window

0
 
JakeSpencerAuthor Commented:
Many thanks for the assistance.  I'll get the book and see if I can brush up on mu skills.

Thanks again.

Lee
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now