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?

Link to comment
Share on other sites

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.');
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.