session ID

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danarashad
Forum Newbie
Posts: 8
Joined: Thu Aug 23, 2007 8:06 am

session ID

Post by danarashad »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


how do i create a session based on the user's ID.  
here's my code i am very, very new to php.  i've only been doing this for 1 day.  so i dont know really what i am doing.  i want to set the ID of the user who logged in as a session variable.  
I figured i could so do so in the if statement, but i have no clue how to do so.  could someone help.  please!!!!!!

Code: Select all

<?php
ob_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="password"; // Mysql password 
$db_name="user"; // Database name 
$tbl_name="user"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);	

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
$_SESSION['views']="$myusername";


header("location:login_success.php");

}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I'm sort of confused... do you want to re-start a previous session? Calling session_start() will pick up your current session already in progress, or start a fresh one if you don't have one going already.

Forget about session_register() - I have a feeling nobody actually uses it. Once the session is going, you can store stuff in the $_SESSION[] superglobal array, and that will remain available for the entire session.

I'd be curious to know who, if any of us, use session_register()... anyone?
Post Reply