Jump to content

pulling session information is it possible?


mbradley

Recommended Posts

Ok i have a simple login and logout with mysql... NOW the question is.. i am NOT using cookies.. but rather

session_register("myusername");
session_register("mypassword");

 

is there a way on the other pages to pull that information... i have on the other pages following....

 

session_start();
if(!session_is_registered(myusername)){
header("location:index.php");
}

 

So any questions PLEASE ask i need to find out how i can access who has logged in and use that to ... well call up the database and relay it to a table and pull only there information?

Link to comment
Share on other sites

Hey,

 

You can always view a sessions contents by using following;

 

echo $_SESSION['myusername'];

 

And use that to select data from that person form the DB; example;

 

$name_q  = "SELECT * FROM users WHERE id='$_SESSION[myusername]'";
$name_r = mysql_query($name_q) or die(mysql_error());

 

So could also use;

 

session_start();
if(empty($_SESSION['myusername'])){
header("location:index.php");
}

 

 

Link to comment
Share on other sites

did you use session_start() before session_register()?

 

well am not using session_register()... what i do is i just directly set/get from the session itself. example:

 

// one.php

<?php

session_start();

 

// set
$_SESSION['user_id'] = $id; // whatever the value maybe 
?>

// two.php
<?php
session_start();

// get
$id = $_SESSION['user_id']; // use it for whatever purpose
?>

 

you can remove all session by session_destroy();

Link to comment
Share on other sites

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:index1.php?name='$myusername");
}
else {
echo "Wrong Username or Password";
}

 

ok so that above... basically there signing in... and well .. dang i will post it up... it all comes from a simple submit forum on index page:)

 

index.php *login*

<?php
session_start();
if(session_is_registered(myusername)){
header("location:index1.php");
}
?>
<html>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>

 

checklogin.php

<?php
ob_start();

$host="****"; // Host name
$username="SecondLifeDB"; // Mysql username
$password="*******"; // Mysql password
$db_name="SecondLifeDB"; // Database name
$tbl_name="members"; // 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'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($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");
header("location:index1.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

 

Now if i see what u put ... then i would change the session_register to just session... i truthfully have not dealt with sessions... i ususally use cookies... but i don't want to use that on this project....

Link to comment
Share on other sites

wahoo i got it .. and it passed to the new page... i appreciate all the help.

 

<?php
ob_start();

$host="***"; // Host name
$username="SecondLifeDB"; // Mysql username
$password="****"; // Mysql password
$db_name="SecondLifeDB"; // Database name
$tbl_name="members"; // 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'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($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){
session_start();

// get
$_SESSION['myusername'] = $myusername; 
// Register $myusername, $mypassword and redirect to file "login_success.php"
header("location:index1.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

 

So anyways yea it worked and it passed so thank you again all...

Link to comment
Share on other sites

session_register(), session_is_registered(), and session_unregister() were depreciated long ago in the year 2002 and they have been completely removed in upcoming php6. Don't use these three functions in your code. Use session_start() and the $_SESSION variable array.

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.