PDA

View Full Version : Cookies or Sessions - PHP help Request~


FirefoxyChic
29th November 10, 11:49 PM
Hi all -

Working on a new site - This time I have to add a registration and login area. I found a very simple and straighforward bit of php code from about.com and decided to implement it on the site. I am not a programmer by any means and merely dabble in simple php. I really need some help and advice from some of you programming gurus here at welovecss.com to get this site running!! THANKS IN ADVANCE!!:):)

A- is this code good using cookies or should I use sessions?
B- If I use sessions instead how difficult would it be to change my existing code

<div class="register">
<?php
// Connects to your Database

mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db("database") or die(mysql_error());


//This code runs if the form has been submitted

if (isset($_POST['submit'])) {



//This makes sure they did not leave any fields blank

if (!$_POST['username'] | !$_POST['password'] | !$_POST['confirmpw'] | !$_POST['lastname_maman'] | !$_POST['firstname_maman'] | !$_POST['email'] | !$_POST['phone']) {

die('You did not complete all of the required fields');

}



// checks if the username is in use

if (!get_magic_quotes_gpc()) {

$_POST['username'] = addslashes($_POST['username']);

}

$usercheck = $_POST['username'];

$check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'")

or die(mysql_error());

$check2 = mysql_num_rows($check);



//if the name exists it gives an error

if ($check2 != 0) {

die('Sorry, the username '.$_POST['username'].' is already in use.');

}


//this makes sure both passwords entered match

if ($_POST['password'] != $_POST['confirmpw']) {

die('Your passwords did not match. ');

}



// here we encrypt the password and add slashes if needed

$_POST['password'] = md5($_POST['password']);

if (!get_magic_quotes_gpc()) {

$_POST['password'] = addslashes($_POST['password']);

$_POST['username'] = addslashes($_POST['username']);

}



// now we insert it into the database

$insert = "INSERT INTO users (lastname_maman, firstname_maman, dob_maman, email, rue, code_postal, city, country, phone, profession_maman, children, lastname_papa, firstname_papa, dob_papa, profession_papa, username, password)

VALUES (
'".$_POST['lastname_maman']."',
'".$_POST['firstname_maman']."',
'".$_POST['dob_maman']."',
'".$_POST['email']."',
'".$_POST['rue']."',
'".$_POST['code_postal']."',
'".$_POST['city']."',
'".$_POST['country']."',
'".$_POST['phone']."',
'".$_POST['profession_maman']."',
'".$_POST['children']."',
'".$_POST['lastname_papa']."',
'".$_POST['firstname_papa']."',
'".$_POST['dob_papa']."',
'".$_POST['profession_papa']."',
'".$_POST['username']."',
'".$_POST['password']."')";

$add_member = mysql_query($insert);

?>

<h1>Registered</h1>

<p>Thank you, you have registered - you may now <a href="login.php">login</a>.</p>



<?php
}

else
{
?>



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<h5>Maman </h5>

<table width="100%">
<tr><td width="28%">
<label for="lastname_maman">Nom <span class="requiredlabel">(requis)</span></label>
</td><td width="22%"><input name="lastname_maman" type="text" id="lastname_maman" value="" class="required" />
</td>
<td width="25%">
<label for="firstname_maman">Pr&eacute;nom<span class="requiredlabel">(requis)</span></label>
</td>
<td width="25%"><input name="firstname_maman" type="text" id="firstname_maman" value="" class="required"/>
</td>
</tr>
<tr><td colspan="2"></td><td colspan="2"></td></tr><tr><td>
<label for="dob_maman">Ann&eacute;e de naissance</label>
</td><td><input name="dob_maman" type="text" id="dob_maman" value="" class="date"/>
</td>

<td><label for="email">Adresse e-mail <span class="requiredlabel">(requis)</span></label>
</td><td><input type="text" name="email" id="email" value="" class="required email" />
</td>

<tr><td colspan="4"></td></tr><tr>
<td>
<label for="rue">Num&eacute;ro et nom de rue</label>
</td><td><input name="rue" type="text" id="rue" value="" />
</td>
<td><label for="code_postal">Code postal</label></td>
<td><input name="code_postal" type="text" id="code_postal" value="" /></tr><tr><td>


</td>
<tr><td colspan="4"></td></tr><tr><td>
<label for="city">Ville</label>
</td><td><input name="city" type="text" id="city" value="" />
</td>

<td>
<label for="country">Pays</label>
</td>
<td colspan="3"><input name="country" type="text" id="country" value="" />
</td></tr>
<tr><td colspan="2"></td>
<td colspan="2"></td><tr ><td>
<label for="phone">T&eacute;l&eacute;phone <span class="requiredlabel">(requis)</span></label>
</td><td><input name="phone" type="text" id="phone" value="" class="required phone"/>
<td>
<label for="profession_maman">Profession</label>
</td><td colspan="3"><input name="profession_maman" type="text" id="profession_maman" value="" />
</td></tr><tr><td colspan="2"></td>

<td colspan="2"></td><tr ><td>
<label for="children">Nombre d'enfants (n&eacute;s et &agrave; na&icirc;tre)</label>
</td><td><input name="children" type="text" id="children" value="" />
</td></tr>

<tr><td colspan="4">&nbsp;</td></tr>
<tr><td colspan="4"><div class="dottedline"></div><h5>Papa</h5></td></tr>

<tr><td width="28%">
<label for="lastname_papa">Nom</label>
</td><td width="22%"><input name="lastname_papa" type="text" id="lastname_papa" value="" />
</td>
<td width="25%">
<label for="firstname_papa">Pr&eacute;nom</label>
</td>
<td width="25%"><input name="firstname_papa" type="text" id="firstname_papa" value="" />
</td>
</tr>
<tr><td colspan="2"></td><td colspan="2"></td></tr><tr><td>
<label for="dob_papa">Ann&eacute;e de naissance</label>
</td><td><input name="dob_papa" type="text" id="dob_papa" value="" />
</td><td>
<label for="profession_papa">Profession</label>
</td><td colspan="3"><input name="profession_papa" type="text" id="profession_papa" value="" />
</td></tr>
<tr><td colspan="4">&nbsp;</td></tr>
<tr><td colspan="4"><div class="dottedline"></div></td></tr>
<td>
<label for="username"> Nom d'utilisateur <span class="requiredlabel">(requis)</span></label>
</td><td><input name="username" type="text" id="username" value="" class="required"/>
</td>
</tr><tr><td colspan="2"></td><td colspan="2"></td></tr><tr><td>
<label for="password">Mot de passe <span class="requiredlabel">(requis - jusqu'&agrave; 7 caract&eacute;res)</span></label>
</td><td><input name="password" type="password" id="password" value="" maxlength="20" class="required"/>
</td>

<td><label for="confirmpw">Confirmer Mot de passe <span class="requiredlabel">(requis)</span></label>
</td><td><input name="confirmpw" type="password" id="confirmpw" value="" class="required"/>
</td>
</tr>

</table>

<p class="submit"><input type="submit" name="submit" class="submit" value="Submit &raquo;" /></p>
</form>
<?php

}
?>
</div>

meesa
30th November 10, 04:16 AM
If you want the user to stay on as long as they are on the site, go with a session (It's really easy) If you want them to be able to come back and still be logged in, then go with a cookie.

chrishirst
30th November 10, 08:49 AM
Sessions ARE cookies that only last for the duration of client/server communications plus a timeout period (20 minutes as default)

FirefoxyChic
30th November 10, 11:56 AM
Soooo sessions are better? Any thoughts on converting this existing code to sessions - Do I just change the word 'cookie' to 'session' or is it more complicated? Thanks guys

meesa
30th November 10, 02:57 PM
Sessions are made in the PHP, and reside on the server.

Cookies are made in the JS, and reside on the clients computer.

The real question is how long you want the user to stay signed in? If when they close the browser window, or they are inactive for X, you want them to be signed out, use a PHP session.

If you want them to stay signed in for a week, use a cookie.

chrishirst
30th November 10, 08:34 PM
Cookies are made in the JSNope Cookies can ALSO be created server-side.

And sessions ALSO reside on the client as well as the server in the form of a cookie (temporary).