Dynamic URL Redirecting for dynamic page

Hi there,

I have a student database system and I am currently adding a simple discussion forum into it. I have no problem to add a link which connected to the forum, but having difficulty to link the forum back to the home page. The system flow is like this; Users login their account and there will be the home page which showing the main menu and their basic info. From the main menu, there is a tab for the forum. At this stage, there is no problem occur. Problem occurred when I redirecting the forum to the home page, it is an empty page which is not the original home page.

Usual home page: URL= http://localhost/Mentor Mentee System/test.php?student_id=12345
Image:

[After redirecting back from forum] Empty home page: URL = http://localhost/Mentor Mentee System/test.php?student_id=
Image:

I need the URL to be “http://localhost/Mentor Mentee System/test.php?student_id=12345” after redirecting from forum.
What can I do to correct that? Please advice.

Don’t pass the student_id in the query string. Use sessions instead.

Like this?

<a href="test.php?student_id=<?php echo $_SESSION['student_id']; ?>"color:# 004080">Home Page</a>

No, what I mean is that once the student has logged in, it’s not a good idea to pass the student id around from one page to another in the query string, because it means a lot of work. You’ll have to add it to the query string of each and every link in your site (which is what is missing in the link back to the main page).
If you save it in a session variable, you’ll always have it ready for use on each page.

If you need help with sessions, you can start by studying the manual, or by googling for a tutorial. And of course, you can ask any questions here if you can’t get it to work.

P.S. the part in red is called the query string:

test.php[B][COLOR="#FF0000"]?student_id=12345[/COLOR][/B]

I still couldn’t get my forum page redirect back to my original logged in user home page.

Here’s the code I wrote for the forum log in page:


<?php require_once('Connections/connection.php'); ?>

<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}
?>
<?php include "incl/hdr.inc";
$j=0;$fx=array();$fy=opu();$fy=explode("\
",$fy);$nick='';
if(isset($nnk)){$nnk=urldecode($nnk);$nnk=explode(":|:",$nnk);$nick=$nnk[2];}
if(isset($tkn)&&!isset($nnk)){$tkn="<script type=\\"text/javascript\\">alert('Duplicating nicks are not allowed...')</script>";}else{$tkn='';}

for($i=0;$i<count($fy);$i++){
if(isset($fy[$i])&&$fy[$i]!=''){$rt=explode(":|:",$fy[$i]);
if($pp-$rt[0]<70&&$nick!=$rt[2]){$fx[$j]=$fy[$i];$j++;}
}}$fx=implode("\
",$fx);wru($fx); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
<link rel="stylesheet" type="text/css" href="incl/nstl.css">
<?php print $crl[1];?><title><?php print $crl[2];?></title>
<script type="text/javascript" src="incl/inx.js"></script>
<?php include "incl/sty.inc";?></head><body onLoad="lo()">
<form action="set.php" method="post"><input type="hidden" value="w1" name="b">
<table align="center" class="tbl" width="330"><tr><td align="center" valign="middle">
<table border="0" width="100%" cellpadding="1" cellspacing="2"><tr><td class="c">
<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td bgcolor="#ffffff">
<table border="0" width="100%" cellpadding="5" cellspacing="1">
<tr><td class="c" align="center"><?php print "$crl[2]";?></td></tr><tr><td class="b">

<table border="0" cellpadding="1" cellspacing="1"><tr>
<td rowspan="2" valign="top" nowrap><b><?php print $crl[24];?> <script type="text/javascript">df()</script>:</b></td>
<td valign="bottom" align="center"><input type="text" size="30" name="n" maxlength="8" class="ia" value="<?php print $nick; ?>"></td>
<td rowspan="2" valign="top"> <input type="submit" value=" OK " class="ib"></td>
</tr><tr><td align="center" valign="top" nowrap>
<a href="#" onClick="wr('w1');return false"><img src="pics/w1.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s1');return false"><img src="pics/s1.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w2');return false"><img src="pics/w2.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s2');return false"><img src="pics/s2.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w3');return false"><img src="pics/w3.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s3');return false"><img src="pics/s3.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w4');return false"><img src="pics/w4.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s4');return false"><img src="pics/s4.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w5');return false"><img src="pics/w5.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s5');return false"><img src="pics/s5.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w6');return false"><img src="pics/w6.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s6');return false"><img src="pics/s6.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w7');return false"><img src="pics/w7.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s7');return false"><img src="pics/s7.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
</td></tr></table></td></tr>
<tr><td class="c" align="center"><?php print "$crl[22]";?></td></tr>
<tr><td class="b" align="center">
<?php $fy=opu();if(strlen($fy)<20){print "<b>$crl[23]</b>";}else{duu(' ');} ?></td></tr>
</table></td></tr></table></td></tr></table></td></tr></table></form><?php print $tkn; ?><tr bgcolor="#FFCCCC">
<?php session_start ();
$_SESSION['student_id']=$student_id;
?>
  <td height="21" colspan="3">&nbsp;&nbsp;&nbsp;<a href="manage_student.php?student_id=<?php echo $_SESSION['student_id']; ?>"color:#004080">Back to home</a></strong></td>
</tr></strong></td>
</tr></body></html>
<?php $fsr=opl();$myar=explode("\
",$fsr);$nr='';
for($i=0;$i<150;$i++){if(isset($myar[$i])){$nr=$nr.$myar[$i]."\
";}}wrl($nr);?>

Please correct where I am did wrongly.

Off Topic:

Holy code-formatting nightmare Batman

You have to use session_start() once per script, on top of each script.
And then you have to set the session variable with the student id once the student has successfully logged in.

Is it like this?

<?php require_once('Connections/connection.php'); ?>

<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}
?>

<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
 // $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>

<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
?>

<?php include "incl/hdr.inc"; 
$j=0;$fx=array();$fy=opu();$fy=explode("\
",$fy);$nick='';
if(isset($nnk)){$nnk=urldecode($nnk);$nnk=explode(":|:",$nnk);$nick=$nnk[2];}
if(isset($tkn)&&!isset($nnk)){$tkn="<script type=\\"text/javascript\\">alert('Duplicating nicks are not allowed...')</script>";}else{$tkn='';}

for($i=0;$i<count($fy);$i++){
if(isset($fy[$i])&&$fy[$i]!=''){$rt=explode(":|:",$fy[$i]);
if($pp-$rt[0]<70&&$nick!=$rt[2]){$fx[$j]=$fy[$i];$j++;}
}}$fx=implode("\
",$fx);wru($fx); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
<link rel="stylesheet" type="text/css" href="incl/nstl.css">
<?php print $crl[1];?><title><?php print $crl[2];?></title>
<script type="text/javascript" src="incl/inx.js"></script>
<?php include "incl/sty.inc";?></head><body onLoad="lo()">
<form action="set.php" method="post"><input type="hidden" value="w1" name="b">
<table align="center" class="tbl" width="330"><tr><td align="center" valign="middle">
<table border="0" width="100%" cellpadding="1" cellspacing="2"><tr><td class="c">
<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td bgcolor="#ffffff">
<table border="0" width="100%" cellpadding="5" cellspacing="1">
<tr><td class="c" align="center"><?php print "$crl[2]";?></td></tr><tr><td class="b">

<table border="0" cellpadding="1" cellspacing="1"><tr>
<td rowspan="2" valign="top" nowrap><b><?php print $crl[24];?> <script type="text/javascript">df()</script>:</b></td>
<td valign="bottom" align="center"><input type="text" size="30" name="n" maxlength="8" class="ia" value="<?php print $nick; ?>"></td>
<td rowspan="2" valign="top"> <input type="submit" value=" OK " class="ib"></td>
</tr><tr><td align="center" valign="top" nowrap>
<a href="#" onClick="wr('w1');return false"><img src="pics/w1.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s1');return false"><img src="pics/s1.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w2');return false"><img src="pics/w2.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s2');return false"><img src="pics/s2.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w3');return false"><img src="pics/w3.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s3');return false"><img src="pics/s3.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w4');return false"><img src="pics/w4.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s4');return false"><img src="pics/s4.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w5');return false"><img src="pics/w5.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s5');return false"><img src="pics/s5.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w6');return false"><img src="pics/w6.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s6');return false"><img src="pics/s6.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('w7');return false"><img src="pics/w7.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
<a href="#" onClick="wr('s7');return false"><img src="pics/s7.gif" width="11" height="14" vspace="6" border="0" alt=""></a>
</td></tr></table></td></tr>
<tr><td class="c" align="center"><?php print "$crl[22]";?></td></tr>
<tr><td class="b" align="center">
<?php $fy=opu();if(strlen($fy)<20){print "<b>$crl[23]</b>";}else{duu(' ');} ?></td></tr>
</table></td></tr></table></td></tr></table></td></tr></table>
</form>
<?php if(isset($_REQUEST['id'])&&($_REQUEST['student_id']=='profile')){?>
<?php print $tkn; ?><tr bgcolor="#FFCCCC">
  <td height="21" colspan="3">&nbsp;&nbsp;&nbsp;<a href="manage_student.php?student_id=<?php echo $_SESSION['student_id']; ?>"color:#004080">Back to home</a></strong></td>
</tr></strong></td>
</tr></body></html>
<?php $fsr=opl();$myar=explode("\
",$fsr);$nr='';
for($i=0;$i<150;$i++){if(isset($myar[$i])){$nr=$nr.$myar[$i]."\
";}}wrl($nr);?>

It prompt me syntax error or the last line. Sorry if I still not get what you trying to say, because this is my 1st project on php and I am trying to learn. Thanks for any advice.

No. Once means once. Not twice.
Like this:


<?php 
  session_start();

  require_once('Connections/connection.php'); 

  $MM_authorizedUsers = "";
  $MM_donotCheckaccess = "true";