Jump to content

Creating cookie with a value from database during login


tuti100

Recommended Posts

I have two tables login and user(all shown below)

 

DROP TABLE IF EXISTS `login`;

CREATE TABLE IF NOT EXISTS `login` (

  `username` varchar(20) NOT NULL default '',

  `password` varchar(20) NOT NULL default '',

  `user_id` int(11) NOT NULL auto_increment,

  PRIMARY KEY  (`username`) 

) TYPE=MyISAM AUTO_INCREMENT=1 ;

 

 

DROP TABLE IF EXISTS `user`;

CREATE TABLE IF NOT EXISTS `user` (

  `user_id` int(11) NOT NULL auto_increment,

  `fname` varchar(20) NOT NULL default '',

  `lname` varchar(20) NOT NULL default '',

  `email` text NOT NULL,

  `user_type` varchar(20) NOT NULL default '',

  `reg_date` date NOT NULL default '0000-00-00',

  `street` varchar(40) NOT NULL default '',

  `building` varchar(40) NOT NULL default '',

  `organisation` varchar(40) NOT NULL default '',

  `department` varchar(40) NOT NULL default '',

  PRIMARY KEY  (`user_id`)

) TYPE=MyISAM AUTO_INCREMENT=1 ;

 

I want when the user logs in, he will be valdated and if he exists in the user table, a cookie will be created with the value of the user_id of the user who logged in. How do i do that and be able to pass the value of the cookie to other pages?

Also how do i prevent unauthorised users from viewing the other pages apart from home page and login page?

How do i achieve pagination while am using a template?

You may create this using sessions as it may be a bit easier but anyway. When the user logins and the validation is successfull, you create the cookie:

setcookie('logged', $username, time()+3600);

The cookie created is called 'logged' and has the value of the username.

 

The cookie is saved in the user's local hard drive so it may be called from what page you like.

 

To not let not logged people access pages of your web sites you can use smth like this:

if(!isset($_COOKIE['logged'])){
    die('You dont have access to this section. Please login first.');
}

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.