Jump to content

Recommended Posts

Anyone know how to use session?  I wanted to do something like this:

 

1. User login in to the system.

2. System authenticate the user according to the username & password that the user have entered earlier.

3. System fetch the user_id of the user & store it in the session.

4. User insert the name, ic & master_id into the system. 

  The master_id value will follow the id stored in the session variable.

 

I use the coding below to authenticate the user and starts a session.

 

 

<?php

session_start();

 

$host="localhost"; // Host name

$username="foong"; // Mysql username

$password="216638"; // Mysql password

$db_name="registration"; // Database name

$tbl_name="user_info"; // Table name

 

// Connect to server and select databse.

$connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

 

$sql="SELECT id, username, password FROM $tbl_name WHERE username='$_POST[username]' and password='$_POST[password]'";

$result=@mysql_query($sql,$connection);

 

 

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

$row = mysql_fetch_array($result);

 

 

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername and redirect to file "login_success.php"

$_SESSION['login'] = 1;

$_SESSION['id'];

header("location:ADMIN.php?id=$row[id]");

}

 

else {

$msg = " Problem login,please try again.......";

header("location:message.php?msg=$msg");

}

 

 

?>

 

As I read through the topic posted, I found out that session_register is no longer being used.

How to pass the session from the first page until the final page?

My session only applicable until the second page. What is the correct way to pass the session?

 

<?php

session_start();

?>

 

Is there anything that I need to specify to pass the session across pages?

Link to comment
https://forums.phpfreaks.com/topic/40114-session-passing-across-pages/
Share on other sites

You don't pass sessions across pages.

 

When you add something to the session, it is there until either the session times out or the user closes the browser.

 

So:

 

session_start();
$_SESSION['variable'] = "test";

 

$_SESSION['variable'] will be stored in the session until as stated "the session times out or the user closes the browser.

"

You don't pass sessions across pages.

 

When you add something to the session, it is there until either the session times out or the user closes the browser.

 

So:

 

session_start();
$_SESSION['variable'] = "test";

 

$_SESSION['variable'] will be stored in the session until as stated "the session times out or the user closes the browser.

"

 

Thanks for your reply.  The "variable" you specified in the coding is the name of the variable and the "test" is the value? Do I need to put this

 

<?php 

 

session_start(); 

 

?>

 

at the heading of every page?

Thanks for the reply.  ;)I have tried the coding but the session can only be passed across the second page only.  I will post the full coding that I have used later on as I don't have it with me now.  Can I insert the session value into the table?  Is this possible?  This is the coding for page 1:

 

<?php

session_start();

 

$host="localhost"; // Host name

$username="foong"; // Mysql username

$password="216638"; // Mysql password

$db_name="registration"; // Database name

$tbl_name="user_info"; // Table name

 

// Connect to server and select databse.

$connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

 

$sql="SELECT id, username, password FROM $tbl_name WHERE username='$_POST[username]' and password='$_POST[password]'";

$result=@mysql_query($sql,$connection);

 

 

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

$row = mysql_fetch_array($result);

 

 

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

  // Register $myusername and redirect to file "login_success.php"

  $_SESSION['login'] = 1;

  $_SESSION['id'];  //Is this correct?

  header("location:ADMIN.php?id=$row[id]");

  }

 

  else {

    $msg = " Problem login,please try again.......";

  header("location:message.php?msg=$msg");

  }

 

 

?>

 

This is what I have in my third page:

 

<?php

session_start();

$master_id == "$_SESSION[id]";    //Is this correct?  The value for the master_id will be the same as the id of the user

..........

 

$sql = "INSERT into $tbl_name (name, grade, master_id) values("$_POST[name]", "$_POST[grade]", "what do I suppose to put here?")";

 

..........

 

?>

 

This is what I want the system to do:

 

1. User login in to the system.

2. System authenticate the user according to the username & password that the user have entered earlier.

3. System fetch the user_id of the user & store it in the session.

4. User insert the name, ic & master_id into the system. 

  The master_id value will follow the id stored in the session variable.

 

coding for login.php

<html>

<head></head>

 

<body>

 

 

<table border="0" width="694" height="292" cellspacing="0">

 

<b><font size="5">LOGIN PAGE</font></b><br><hr></tr>

<center>

<br><br><td width="1000" height="226" align="center" valign="top">

        <table width="300" height="50" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

        <td colspan="3"><strong><center><font size = "5">ADMIN LOGIN</font></center></strong></td>

</tr>

 

<tr>

<form  method="post" action="CHECKLOGIN.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="4" bgcolor="#FFFFFF">

 

<tr>

<td width="78">USERNAME</td>

<td width="6">:</td>

<td width="294"><input name="username" type="text" ></td>

</tr>

 

<tr>

<td>PASSWORD</td>

<td>:</td>

<td><input name="password" type="password" ></td>

</tr>

 

<tr>

<td> </td>

<td> </td>

<td><input type="submit" name="Submit" value="Login"></td>

</tr>

 

</table>

</td>

</form>     

 

</body>

</html>

 

 

coding for checkLogin.php

<?php

session_start();

 

$host="localhost"; // Host name

$username="foong"; // Mysql username

$password="216638"; // Mysql password

$db_name="registration"; // Database name

$tbl_name="user_info"; // Table name

 

// Connect to server and select databse.

$connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

 

$sql="SELECT id, username, password FROM $tbl_name WHERE username='$_POST[username]' and password='$_POST[password]'";

$result=@mysql_query($sql,$connection);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

$row = mysql_fetch_array($result);

 

 

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername and redirect to file "login_success.php"

$_SESSION['login'] = 1;

$_SESSION['id'];

header("location:INSERT_RECORD.php?id=$row[id]");

}

 

else {

$msg = " Problem login,please try again.......";

header("location:message.php?msg=$msg");

}

 

coding for insert_record.php

<?php

session_start();

 

if(isset($_SESSION['login']))

echo "";

else

{

  header("location: LOGIN.php");

}

 

 

$id == $_SESSION['id'];

echo "ID = $id";

 

?>

 

<html>

<head></head>

 

<body>

 

 

<table border="0" width="694" height="292" cellspacing="0">

 

<b><font size="5">INSERT PAGE</font></b><br><hr></tr>

<center>

<br><br><td width="1000" height="226" align="center" valign="top">

        <table width="300" height="50" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

        <td colspan="3"><strong><center><font size = "5">INSERT PAGE</font></center></strong></td>

</tr>

 

<tr>

<form  method="post" action="doINSERT_DETAILS.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="4" bgcolor="#FFFFFF">

 

<tr>

<td width="78">NAME</td>

<td width="6">:</td>

<td width="294"><input name="name" type="text" ></td>

</tr>

 

<tr>

<td>GRADE</td>

<td>:</td>

<td><input name="grade" type="text" ></td>

</tr>

 

<tr>

<td> </td>

<td> </td>

<td><input type="submit" name="Submit" value="SUBMIT"></td>

</tr>

 

</table>

</td>

</form>

   

 

</body>

</html>

 

 

coding for doinsert_details.php

<?

session_start();

 

$id == $_SESSION['id'];

echo "ID = $id";

 

 

$host="localhost"; // Host name

$username="TEST"; // Mysql username

$password="TEST"; // Mysql password

$db_name="registration"; // Database name

$tbl_name="bhgn1"; // table name

 

// Connect to server and select databse.

$connection=@mysql_connect("$host", "$username", "$password")or die("cannot connect");

$db=@mysql_select_db("$db_name")or die("cannot select DB");

 

$master_id = $_SESSION[id];

 

$sql="INSERT INTO $tbl_name (master_id,name,grade)

VALUES ('$_SESSION[id]','$_POST[name]','$_POST[grade]')";

 

 

 

$result=@mysql_query($sql,$connection) or die(mysql_error());

 

?>

 

<head></head>

 

<body>

<center>

 

<table border="0" width="694" height="292" cellspacing="0">

<form method="post" action="INSERT_MAIN.php">

<tr>

 

      <td width="1000" height="226" valign="top"><br>

  <b><font size="5">NEW RECORD ADDED INTO <? echo "$tbl_name" ?></font></b><br><hr>

 

<br><br><b><? echo "$_SESSION[id]</b>  has been inserted into

<b>$tbl_name</b>";?><br><br><br>

 

<INPUT TYPE="SUBMIT" NAME="submit" VALUE="INSERT ANOTHER RECORD">

 

  </table>

 

</body>

</html>

 

Anyone have any idea what is wrong with the codes?

 

 

 

?>

 

 

 

 

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.