Jump to content

PHP Cookies


Drezard

Recommended Posts

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

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.