954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

passing session

Hello I want to pass the database retrieved username after user logs on & hence to check login correctness I am using php & jsp

<?php
$conn= mysql_connect("localhost", "root", "");
  if(!$conn){
      exit("Error in SQL");
    }
  mysql_select_db("college", $conn);
  $val1=$_REQUEST['login1'];
  $val2=$_REQUEST['password1'];
  
  $sql = "SELECT * FROM tlogin WHERE tid='$val1' AND tpasswd='$val2'";
  $rt = mysql_query($sql, $conn);
  $num = mysql_num_rows($rt);

  if($num==0){
    print("<script language='javascript'>");
  print("window.location='index.html';");
  print("alert(\"Incorrect username or password...\")");
  print("</script>"); 
  } 
  else
  {
  
  mysql_select_db("college", $conn);
  $row= mysql_fetch_array($rt);
  $tname=$row['$tname'];
  $_POST['$tname'];
  URL=http://172.31.81.150/teacher.php?exvar='$tname';
  //$sql = "SELECT sname FROM register WHERE regno=$val1";
  //$rt=mysql_query($sql,$conn);
  //$a_row = mysql_fetch_array( $rt );
  //$sname=stripslashes($a_row['sname']);
  //echo urlencode($_GET['sname']);
   
  //session_register("username");

//username = "$sname";
 //$_SESSION[$sname]="$sname";
  setcookie("auth", "1", 0, "/", "", 0);
  //print("<script language='javascript'>"); 
    
  //print("window.location='teacher.php';"); 
  //print("</script>");
 }
?>

But am unable to transfer the usrname so that I can welcome him in next page

s s paul
Newbie Poster
10 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
s s paul
Newbie Poster
10 posts since Aug 2006
Reputation Points: 10
Solved Threads: 1
 

I guess a simple example would be...

pass.php

<a href="temp.php?var=hello">test</a>


temp.php

<?
echo $_GET['var'];
?>



Which should display the word hello, which is passed from pass.php (the first url) to the second url.

Straight from php man http://ca3.php.net/variables.external

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Before you can use. set, unset, etc any session variables you must first

<? session_start(); ?>


on each page using sessions.
This is required and no session vars can be used otherwise. Its good practice to have at the top of your pages so you don't forget it.

Assuming that your query is pulling the correct data, define the session variable without the dollar sign and with single quotes like:

$_SESSION['sname'] = $sname;


.

Retrieving it to display on any other page is now as simple as

echo $_SESSION['sname'];


or for use however.

doubledub
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You