Solved

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

Posted on 2011-02-21
11
807 Views
Last Modified: 2012-05-11
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
Comment
Question by:JakeSpencer
  • 5
  • 3
  • 3
11 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34942226
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
 

Author Comment

by:JakeSpencer
ID: 34942275
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
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34942309
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
 

Author Comment

by:JakeSpencer
ID: 34942354
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
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 125 total points
ID: 34942437
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 375 total points
ID: 34943432
0
 

Author Comment

by:JakeSpencer
ID: 34943803
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 375 total points
ID: 34943922
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
 

Author Comment

by:JakeSpencer
ID: 34943991
That looks great.  How do I give it the database name to write the name and email address to?

Many Many thanks
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 375 total points
ID: 34944524
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
 

Author Comment

by:JakeSpencer
ID: 34949490
Many thanks for the assistance.  I'll get the book and see if I can brush up on mu skills.

Thanks again.

Lee
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now