WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases Update Picture
Wild Karlheinz
Date : Jan 17th 1999
Grade : 1 of 5 (graded 1 times)
Viewed : 13506
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Wild Karlheinz
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

<?php
# -------------------------------------------------------------------
# Session Management v1.0 21.6.1998
# (c) Wild Karl Heinz <[email protected]>
#
# This Include handle Session based variable handling
#
# Please feel free and use it. If you make it more functional
# it would be nice to send me a copy.
#
# Don't forget - Mysql_connect !
#
# The database structure
# Table structure for table 'session'
#
# CREATE TABLE session (
# id int(11) DEFAULT '0' NOT NULL auto_increment,
# sid varchar(20) DEFAULT '' NOT NULL,
# val blob,
# times timestamp(14),
# PRIMARY KEY (id),
# KEY sid (sid),
# UNIQUE sid_2 (sid)
# );
#
# You'll miss here a cron job to delete the old sessions from db
# -------------------------------------------------------------------

$sess_db = 'test';
$sess_table = 'session';

# ----------------------------------------------------
# Session_CheckID - Get or Set the Session-ID
# Parameter.: time how long the cookie will keept
# or null if it's only a session cookie
# Return....: Session-Unique ID
# ----------------------------------------------------
function Session_CheckID( $min )
{
global $sess_sid;

if( !$sess_sid ) {
$sess_sid = uniqid( SC );
if( $min > 0 ) {
SetCookie("sess_sid", $sess_sid, time()+($min*60), "/", "", 0 );
}
else {
SetCookie("sess_sid", $sess_sid, "", "/", "", 0 );
}
return( false );
}
else {
return( $true );
}
}

# ----------------------------------------------------------
# str2arr - build out from a string with eval the new array
# parameter.: string
# returns...: global array
# ----------------------------------------------------------
function str2arr( $ts )
{
global $session;

$vals = split( "&", $ts );
while( list($key,$val) = each($vals) ) {
list( $name, $wert ) = split( "=", $val );
if( $val ) eval( "\$$name = \"$wert\";" );
}
}

# ----------------------------------------------------------
# session_read - reads the session-variables
# Parameter.: none
# returns...: read - ok = true
# ----------------------------------------------------------
function session_read()
{
# Hash array to keep session-variables
global $session;

global $sess_sid, $sess_db, $sess_table, $sess_error;


$sel = "Select val from $sess_table where sid = '$sess_sid'";
$res = mysql_db_query( $sess_db, $sel );
if( mysql_numrows( $res ) ) {
$val = mysql_result( $res, 0, "val" );
str2arr( $val );
mysql_free_result( $res );
return( true );
}
else {
return( false );
$sess_error = mysql_error();
}
}

# ------------------------------------------------------
# Split_Array - reads the session-array into a string
# Parameter.: array
# returns...: string with & separeted array fields
#
# Thanks to Rasmus
# ------------------------------------------------------
function Split_Array( $arr, $a = "", $b = "", $c = "" )
{
while( list( $key, $val ) = each( $arr ) ) {
if( is_array( $val ) ) {
$ts .= Split_Array( $arr[ $key ],
( strlen( $a ) ? $a : $key ),
( strlen( $b ) ? $b : ( strlen( $a ) ? $key : "" ) ),
( strlen( $c ) ? $c : ( strlen( $b ) ? $key : "" ) ) );
}
else {
$ts .= "session";
$ts .= $a ? "[$a]" : "";
$ts .= $b ? "[$b]" : "";
$ts .= $c ? "[$c]" : "";
$ts .= "[$key]=$val&";
}
}
return( $ts );
}

# ---------------------------------------------------
# session_write - writes the session-variable from
# the array session
# parameter.: none
# returns...: write - ok = true
# ---------------------------------------------------
function session_write()
{
# Hash array to keep session-variables
global $session;

global $sess_sid, $sess_db, $sess_table;
global $sess_error;

# if you like to delete a session-cookie
# you must check it before writting the session
# array

if( !$sess_sid ) { session_checkid( 0 ); }

$ts = Split_Array( $session );
if( $ts > "" ) { $ts = substr( $ts, 0, strlen( $ts ) - 1 ); }
$res = mysql_db_query( $sess_db, "Select * from session where sid = '$sess_s'");
if( mysql_numrows( $res ) == 0 ) {
$sel = "Insert into $sess_table ( id, sid, val, times ) ";
$sel .= "values( 0, '$sess_sid', '$ts', NULL )";
}
else {
$sel = "Update $sess_table set val = '$ts', ";
$sel .= "times = NULL where sid = '$sess_sid'";
}
if( !mysql_db_query( $sess_db, $sel ) ) {
$sess_error = mysql_error();
return( false );
}
else { return( true ); }
}

# ---------------------------------------------
# session_del - clears an entry
# parameter.: hash - id
# returns...: none
# ---------------------------------------------
function session_del()
{
global $session, $sess_db, $sess_table, $sess_sid;

$sel = "Delete from $sess_table where sid = '$sess_sid'";
if( !mysql_db_query( $sess_db, $sel ) ) {
$sess_error = mysql_error();
}
$sess_sid = '';
}

?>

[------------------ This is an example ------------------]

<?php
require( "session.inc" );

if( $del ) {
session_del();
}

session_checkid( 0 );

mysql_connect('') or Die("can't connect to db!");

# Normal use ist to read the session-var and assign it to the
# working vars. This example assign after session-read fail the
# new values.
if( session_read() ) {
$hallo = $session[hallo];
$w12 = $session[w12];
$arr = $session[arr];
}
else {
for( $i = 1; $i <= 10; $i++ ) {
for( $j = 1; $j <= 2; $j++ ) {
$arr[$i][$j] = $i+$j;
}
}

$w12 = '10232';
$hallo = 'Ho oh. ';
}

?>
<html>
<head><title>Session/Cookie-Test 1</title>
</head>

<body>

<h2>This Page should show how to handle the "session.inc" library</h2>

<h3>We will use a mask with a record showing routine</h3>

<?php
print "<h4>Show all variables</h4>";
for( $i = 1; $i <= 10; $i++ ) {
print "\$arr: [$i][1-2] = " . $arr[$i][1] . " / " . $arr[$i][2] . "<br>";
}
print "<br>";
print "w12: " . $w12 . "<br>";
print "hallo: " . $hallo . "<br>";

# increment variables
for( $i = 1; $i <= 10; $i++ ) {
for( $j = 1; $j <= 2; $j++ ) {
$arr[$i][$j] += 2;
}
}

$w12++;
$hallo .= "w1";

# -------------------------------------
# reassign session variables
# -------------------------------------
$session[arr] = $arr;
$session[w12] = $w12;
$session[hallo] = $hallo;

# -------------------------------------
# store session variables
# -------------------------------------
if( !session_write() ) {
print $sess_error;
}

?>

<form action=sess1.php3 method=post>
<hr>
<input type=submit name=del value=" reset session ">

If you like to reset the session - click

</form>

</body>
</html>




Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
Finds the median in an array of numbers - Can be used with a MySql database column read into an array
Categories : PHP, Arrays, Databases, MySQL
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses a 'multi-dimension array', but not really as the array is just one big array with the index of "[$i][$j]". Have a look at the code and you'll see what I mean.
Categories : PHP, MySQL, Arrays, Databases
Example voting script. Lets people enter suggestions and vote for existing ones.
Categories : MySQL, PHP, Cookies, Complete Programs, Databases
How to load a query result into a PHP Array
Categories : PHP, Databases, Arrays, MySQL
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query
Categories : Databases, PHP, MySQL, Arrays
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Function for retrieving MySQL enum values into a PHP array.
Categories : PHP, Databases, MySQL, Arrays
A very simple and efficient split bar the B-Z bar , for mysql and php ... Tired of obfuscated code try this one ...
Categories : PHP, Databases, MySQL, Algorithms
How to Insert a Date Format Into MySQL from PHP
Categories : PHP, Databases, MySQL, Date Time, Beginner Guides
Add, Edit /Update & Delete all in one Contact Management Form
Categories : PHP, MySQL, Databases, Beginner Guides
AITSH Download
Categories : PHP, Complete Programs, MySQL, Databases