Link to home
Start Free TrialLog in
Avatar of edvinson
edvinsonFlag for United States of America

asked on

How do I set a session on a login page so that the page it sends to picks it up?

I am told that my login page needs to set this: $_SESSION['username'] so the next page picks it up to be able to display "Welcome to the logged in user"

My question is, where on the login page do I need to put what in order to do that?

Use username: test      password: password

I'll attach code following for the login page and for the redirected page.

Please help and please talk real slow and if possible, in Dreamweaver language.

Thanks

<?php require_once('Connections/PlugASong.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  session_start();
}
 
$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
 
if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "index2.php";
  $MM_redirectLoginFailed = "loginfailure.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_PlugASong, $PlugASong);
  
  $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $PlugASong) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      
 
    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PlugASong.com and FindAGreatsong.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- ImageReady Preload Script (indexmain.psd) -->
<script type="text/javascript">
<!--
 
userAgent = window.navigator.userAgent;
browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}
 
function findElement(n,ly) {
	if (browserVers < 4)		return document[n];
	var curDoc = ly ? ly.document : document;
	var elem = curDoc[n];
	if (!elem) {
		for (var i=0;i<curDoc.layers.length;i++) {
			elem = findElement(n,curDoc.layers[i]);
			if (elem) return elem;
		}
	}
	return elem;
}
 
function changeImages() {
	if (document.images && (preloadFlag == true)) {
		var img;
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			img = null;
			if (document.layers) {
				img = findElement(changeImages.arguments[i],0);
			}
			else {
				img = document.images[changeImages.arguments[i]];
			}
			if (img) {
				img.src = changeImages.arguments[i+1];
			}
		}
	}
}
 
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		indexmain_03_over = newImage("images/indexmain_03-over.gif");
		indexmain_05_over = newImage("images/indexmain_05-over.gif");
		indexmain_06_over = newImage("images/indexmain_06-over.gif");
		indexmain_07_over = newImage("images/indexmain_07-over.jpg");
		preloadFlag = true;
	}
}
 
// -->
</script>
<!-- End Preload Script -->
<!-- ImageReady Styles (indexmain.psd) -->
<style type="text/css">
body {
	padding-top:0;
	margin-top:0;
	background-color: #000000;
	background-image: url(images/blueline.jpg);
}
<!--
 
#Table_01 {
	position:absolute;
	left:0px;
	top:0px;
	width:800px;
	height:933px;
	padding: 0px;
	
}
#entirebody {
	position: relative;
	
}
 
#indexmaintop_ {
	position:absolute;
	left:0px;
	top:0px;
	width:800px;
	height:150px;
}
 
#indexmainleftstrip_ {
	position:absolute;
	left:0px;
	top:150px;
	width:25px;
	height:783px;
}
 
#indexmain-03_ {
	position:absolute;
	left:25px;
	top:150px;
	width:130px;
	height:27px;
}
 
#indexmainbody_ {
	position:absolute;
	left:155px;
	top:150px;
	width:645px;
	height:783px;
}
 
#indexmain-05_ {
	position:absolute;
	left:25px;
	top:177px;
	width:130px;
	height:27px;
}
 
#indexmain-06_ {
	position:absolute;
	left:25px;
	top:204px;
	width:130px;
	height:26px;
}
 
#indexmain-07_ {
	position:absolute;
	left:25px;
	top:230px;
	width:130px;
	height:30px;
}
 
#indexmainleft_ {
	position:absolute;
	left:25px;
	top:260px;
	width:130px;
	height:673px;
}
.style1 {	font-family: Geneva, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
.style2 {
	font-family: Georgia, "Times New Roman", Times, serif;
	font-size: 12px;
	color: #FFFFFF;
}
.style3 {
	font-family: Georgia, "Times New Roman", Times, serif;
	color: #FFFFFF;
}
#form1 {	z-index: 10;
	position: absolute;
	top: 275px;
	left: 25px;
}
.style4 {color: #FFFFFF}
 
-->
</style>
<!-- End ImageReady Styles -->
</head>
<body onload="preloadImages();" style="background-color:#666666;">
<!-- ImageReady Slices (indexmain.psd) -->
<div id="entirebody" style="width: 800px; margin: 0px auto; ">
  <div id="Table_01">
  <form ACTION="<?php echo $loginFormAction; ?>" id="form1" name="form1" method="POST">
  <label> <span class="style1"><span class="style3">Login </span><span class="style4">&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
  <input name="username" type="text" id="username" />
  <br />
  </label>
  <p> <span class="style2">Password</span>
      <input name="password" type="text" id="password" />
      <br />
      <br />
      <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
 
	<div id="indexmaintop_">
		<a href="index.php">
			<img id="indexmaintop" src="images/indexmaintop.gif" width="800" height="150" border="0" alt="" /></a>
	</div>
	<div id="indexmainleftstrip_">
		<img id="indexmainleftstrip" src="images/indexmainleftstrip.gif" width="25" height="783" alt="" />
	</div>
	<div id="indexmain-03_">
		<a href="index.php"
			onmouseover="window.status='Return to Home Page'; changeImages('indexmain_03', 'images/indexmain_03-over.gif'); return true;"
			onmouseout="window.status=''; changeImages('indexmain_03', 'images/indexmain_03.gif'); return true;"
			onmousedown="changeImages('indexmain_03', 'images/indexmain_03-over.gif'); return true;"
			onmouseup="changeImages('indexmain_03', 'images/indexmain_03-over.gif'); return true;">
			<img name="indexmain_03" id="indexmain_03" src="images/indexmain_03.gif" width="130" height="27" border="0" alt="" /></a>
	</div>
	<div id="indexmainbody_">
		<img id="indexmainbody" src="images/indexmainbody.gif" width="645" height="783" alt="" />
	</div>
	<div id="indexmain-05_">
		<a href="library.php"
			onmouseover="window.status='Search the Song Library'; changeImages('indexmain_05', 'images/indexmain_05-over.gif'); return true;"
			onmouseout="window.status=''; changeImages('indexmain_05', 'images/indexmain_05.gif'); return true;"
			onmousedown="changeImages('indexmain_05', 'images/indexmain_05-over.gif'); return true;"
			onmouseup="changeImages('indexmain_05', 'images/indexmain_05-over.gif'); return true;">
			<img name="indexmain_05" id="indexmain_05" src="images/indexmain_05.gif" width="130" height="27" border="0" alt="" /></a>
	</div>
	<div id="indexmain-06_">
		<a href="songwriters.php"
			onmouseover="window.status='Songwriters'; changeImages('indexmain_06', 'images/indexmain_06-over.gif'); return true;"
			onmouseout="window.status=''; changeImages('indexmain_06', 'images/indexmain_06.gif'); return true;"
			onmousedown="changeImages('indexmain_06', 'images/indexmain_06-over.gif'); return true;"
			onmouseup="changeImages('indexmain_06', 'images/indexmain_06-over.gif'); return true;">
			<img name="indexmain_06" id="indexmain_06" src="images/indexmain_06.gif" width="130" height="26" border="0" alt="" /></a>
	</div>
	<div id="indexmain-07_">
		<a href="songseekers.php"
			onmouseover="window.status='Seeking Music?'; changeImages('indexmain_07', 'images/indexmain_07-over.jpg'); return true;"
			onmouseout="window.status=''; changeImages('indexmain_07', 'images/indexmain_07.jpg'); return true;"
			onmousedown="changeImages('indexmain_07', 'images/indexmain_07-over.jpg'); return true;"
			onmouseup="changeImages('indexmain_07', 'images/indexmain_07-over.jpg'); return true;">
			<img name="indexmain_07" id="indexmain_07" src="images/indexmain_07.jpg" width="130" height="30" border="0" alt="" /></a>
	</div>
	<div id="indexmainleft_">
		<img id="indexmainleft" src="images/indexmainleft.gif" width="130" height="673" alt="" />
	</div>
</div>
<!-- End ImageReady Slices --></div>
</body>
</html>

Open in new window

index2.txt
Avatar of Hube02
Hube02
Flag of United States of America image

I've copied the portion of your script where the login happens and commented the bid where login is successfully as well as a comment where the session variables are set on a successful login.

Let me know if you have any questions.
f (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "index2.php";
  $MM_redirectLoginFailed = "loginfailure.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_PlugASong, $PlugASong);
  
  $LoginRS__query=sprintf("SELECT username, password FROM users WHERE username=%s AND password=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $PlugASong) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
 
 
     // *** LOGIN SUCCESS HERE *** //
 
 
     $loginStrGroup = "";
    
    //declare two session variables and assign them
 
 
    $_SESSION['MM_Username'] = $loginUsername;
    // *** $_SESSION['MM_Username'] contains the users name *** ///
    // *** YOU CAN ADD OTHER SESSION VARIABLES HERE *** ///
 
 
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      
 
    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}

Open in new window

Avatar of edvinson

ASKER

I tried pasting your code into lines 41-72 but Dreamweaver threw an error showing there was two Login User Behaviors rather than 1.

Hopefully I did it on the right page which was login1.php but it didn't work.
ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so very much. You intervened while I still had some hair left!