Drezard Posted January 3, 2007 Share Posted January 3, 2007 Hello, Here is my code for my class.php file. This file contains all of my functions to be used inside my PHP applications.I want to set a cookie there with the $user and $pass function where the comment is: [QUOTE]// set cookie here[/QUOTE] Here my code:[CODE]<?php/* * Mysql.php * * This class is meant to do most of the interaction with the MySQL database. * * Last updated: December 16, 2006. */ /* * This class contains these functions: * * connect() * */ include ('constants.php'); /* * MySQLDB contains most of the functions to interact with the MySQL database. */ class MySQLDB { var $connection; // Contains all the connection to MySQL information function connect() { // Connects to the MySQL database $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME, $this->connection) or die(mysql_error()); } /* * This function is used to check the username and password from a form against the one in the database. * * This function is used at login. * */ function check_username($user, $pass) { $query = "SELECT * FROM user_ids WHERE user='$user' AND pass='$pass'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); $count = mysql_num_rows($result); if ($count == 1) { return 0; // set cookie here } if ($count == 0) { return 1; } } /* * This function is used to check the username and password from a cookie or session against the one in the database. * * This function is used on every page to see if a user is logged in. */ function check_userid($userid, $pass) { $query = "SELECT * FROM user_ids WHERE userid='$userid' AND pass='$pass'"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); $count = mysql_num_rows($result); if ($count == 1) { return 0; } if ($count == 0) { return 1; } } /* * This function contains the form to get the user to login. * * This function is used when you want to login */ function user_form() { if (!isset($_SESSION['userinfo'])) { echo "Please login:<br><br>"; echo "<form action='' method='post'> Username: <input type='text' name='user'><br> Password: <input type='password' name='pass'><br> <input type='submit' name='submit'> </form>"; $user = $_POST['user']; $pass = $_POST['pass']; $this->check_username($user, $pass); } if (isset($_SESSION['userinfo'])) { echo "You are already logged in"; } }}?>[/CODE]How do I set a cookie there?- Cheers, Daniel Link to comment https://forums.phpfreaks.com/topic/32672-php-cookies/ Share on other sites More sharing options...
chronister Posted January 3, 2007 Share Posted January 3, 2007 From the PHP manual...[code]<?php$value = 'something from somewhere';setcookie("TestCookie", $value);setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);?> [/code] Link to comment https://forums.phpfreaks.com/topic/32672-php-cookies/#findComment-152057 Share on other sites More sharing options...
Drezard Posted January 3, 2007 Author Share Posted January 3, 2007 Yea, then it gives me the an error. Headers already sent.- Cheers, daniel Link to comment https://forums.phpfreaks.com/topic/32672-php-cookies/#findComment-152124 Share on other sites More sharing options...
trq Posted January 3, 2007 Share Posted January 3, 2007 read [url=http://www.phpfreaks.com/forums/index.php/topic,37442.0.html]this[/url] thread on the subject. Link to comment https://forums.phpfreaks.com/topic/32672-php-cookies/#findComment-152130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.