Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-21
11
Medium Priority
?
835 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 500 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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1500 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1500 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

610 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