Jump to content

[SOLVED] Saving Data in Cookies ...


bsamson

Recommended Posts

Hello. I am fairly new to cookies. I have basically 3 pages at this point.

 

Login.php

Validate.php

front.php

 

A user goes to the Login page and provides their credentials. The Validate page looks like this:

 

<?php
require_once("common.php"); // Loads ConnectTo Function

$connection = ConnectTo("main");

$query = "SELECT * FROM employees WHERE uName='$_POST[uName]' AND pWord='$_POST[pWord]'";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0) { 
header("Location: http://mydomain.com/Login.php?err=1"); 
} else {

while ($row=mysql_fetch_array($result)) {
setcookie("sns_fname", $row['fname'], time()+3600);
setcookie("sns_lname", $row['lname'], time()+3600);
setcookie("sns_uName", $row['uName'], time()+3600);
setcookie("sns_added", $row['added'], time()+3600);
setcookie("sns_updated", $row['updated'], time()+3600);
setcookie("sns_lastlogin", $row['lastlogin'], time()+3600);
}

header("Location: http://mydomain.com/front.php");
}
?>

 

By the way, if i comment out the header line from Login.php, and put in this line:

<?php echo $_COOKIE['sns_fname']; ?>

It displays the name.

 

if the username & password were found the user ends up @ front.php. Now, on front.php I try this:

<?php echo $_COOKIE['sns_fname']; ?>

 

And it doesn't display the name. Am i missing something obvious? Thanks in advance for any assistance!

Link to comment
https://forums.phpfreaks.com/topic/115050-solved-saving-data-in-cookies/
Share on other sites

Ok, now that you edited your post, do some simple troubleshooting.  First make sure the cookie value is being set.  Don't use a MYSQL result for it, use something you know works..

 

setcookie("sns_fname", "Fred", time()+3600);

 

Change

 

header("Location: http://mydomain.com/front.php");

 

to

 

header("Location: front.php");

 

You are probably setting the cookie on www.mydomain.com and then you redirect to mydomain.com.  Use Relative paths, not absolute.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.