Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy variable across URLs 1

Status
Not open for further replies.

Sitehelp

Technical User
Joined
Feb 4, 2004
Messages
142
Location
GB
If anyone can help with this I would be SOOO appreciative!

I am creating an online Help System using Dreamweaver MX, and I am creating it using PHP. Up until now all was going fine. The user can log in and it verifies their password in the database and then copies the clientID into the URL so it can be retrieved on the next page, however, once you have logged in I want it to be able to recognise the user on all the pages thus needing the ClientID to pass through all the URLs, or at least I presume this is the best and easiest way. I can get it through to one, when copying from the log in to the next page, as its done through a submit button, but cannot copy it to the other pages at all. I have imported buttons from Fireworks and created a navigation bar with them, which is in JavaScript I believe. Any ideas how I could get these buttons to copy the ClientID from the URL into the next URL when I click a link. I am using Dreamweaver MX by the way, if that makes any difference. Any help will be MUCH appreciated, hope this all made sense!!!!
 
Sorry abour that I sent the wrong code! I meant to send:

if (!isset($_SESSION['ClientID'])&& isset($_POST['ClientID'])){

$SQL = "SELECT ClientID FROM clientinfo
WHERE ClientID ='".$_POST['$ClientID']."'
AND Cpassword = '".$_POST['$fPassword']."'";
$result = mysql_query($SQL) OR die("Error: MySQL said ".mysql_error());
$row = mysql_fetch_assoc($result);

if ($row['num'] == 1)
header("location: Logged In/WelcomeUserPage.php");
} else {
print "Details either incorrect or not submitted";
$_SESSION['ClientID'] = $_POST['ClientID'];

Its posting through to the next page regardless of whether the SQL is correct? any ideas?
 
It's always a good idea to inspect what the vars actually are.
Insert a print_r statement for the POST and SESSION vars at the top of the script:
Code:
print &quot;<pre>&quot;;
print_r($_POST);
print_r($_SESSION);
print &quot;</pre>&quot;;

This will dump all vars in the specified arrays. You could see all globals and superglobals by print_r($GLOBALS) - that will show you the entire environment your script is working in.
See if the values you expect are there. Let's go from here.
 
ok I have inserted the print_r statements just above the code I printed out in the last message. Its returning:

Array
(
)

I presume values are supposed to be shown? I have used print_r($GLOBALS) which does return the whole scripting environment (can paste in if easier?). My code now looks like:

<?php require_once('../Connections/MARTIN.php'); ?>
<?php
if ((isset($HTTP_POST_VARS[&quot;MM_insert&quot;])) && ($HTTP_POST_VARS[&quot;MM_insert&quot;] == &quot;form3&quot;)) {
$insertSQL = sprintf(&quot;INSERT INTO loggedindetails (ClientID) VALUES (%s)&quot;,
GetSQLValueString($HTTP_POST_VARS['ClientID'], &quot;text&quot;));

mysql_select_db($database_MARTIN, $MARTIN);
$Result1 = mysql_query($insertSQL, $MARTIN) or die(mysql_error());


if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? &quot;&&quot; : &quot;?&quot;;
$insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
}
header(sprintf(&quot;Location: %s&quot;, $insertGoTo));
}

$colname_Recordset1 = &quot;1&quot;;
if (isset($HTTP_GET_VARS['ClientID'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['ClientID'] : addslashes($HTTP_GET_VARS['ClientID']);
}
mysql_select_db($database_MARTIN, $MARTIN);
$query_Recordset1 = sprintf(&quot;SELECT ClientID FROM clientinfo WHERE ClientID = '%s'&quot;, $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $MARTIN) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
print &quot;<pre>&quot;;
print_r($_POST);
print_r($_SESSION);
print &quot;</pre>&quot;;
//print_r($GLOBALS);
# check if not logged in
if (!isset($_SESSION['ClientID'])&& isset($_POST['ClientID'])){

$SQL = &quot;SELECT ClientID, cpassword FROM clientinfo
WHERE ClientID ='&quot;.$_POST['$ClientID'].&quot;'
AND cpassword = '&quot;.$_POST['$fpassword'].&quot;'&quot;;

$result = mysql_query($SQL) OR die(&quot;Error: MySQL said &quot;.mysql_error());
$row = mysql_fetch_assoc($result);

// if num is 1 -> go ahead, 0 -> wrong pw/user
if ($row['num'] == 1)
header(&quot;location: Logged In/WelcomeUserPage.php?ClientID=&quot;.$HTTP_POST_VARS['ClientID'].&quot;&quot;);
// ... redirect to destination ...
} else {
print &quot;Details either incorrect or not submitted&quot;;
// ... error message, try again ...
} // $_SESSION['ClientID'] = $_POST['ClientID'];

// ... database code here ...
# if user ok
$_SESSION['ClientID'] = $_POST['ClientID'];

# now redirect

# form was not posted -> draw it
//print &quot;<html><body>\n&quot;;
//print '<form name=&quot;login&quot; action=&quot;'.$_SERVER[PHP_SELF].'&quot; method=&quot;post&quot;>';
//print 'Username : <input type=&quot;text&quot; name=&quot;ClientID&quot;><br>';
//print 'Password : <input type=&quot;password&quot; name=&quot;fpassword&quot;><br>';
//print '<input type=&quot;submit&quot; value=&quot;Submit&quot;></form></body></html>';


//elseif ($num ==1)
//header (&quot;location: User Section/UserHomePage.php&quot;);
// else
// $message = &quot;<i>The details you have entered are incorrect&quot;;
//}
//$sql = &quot;SELECT password FROM clientinfo // 26
// WHERE password=password('$fpassword')&quot;;
// $result2 = mysql_query($sql)
// or die(&quot;Couldn't execute query.&quot;);
// $num2 = mysql_num_rows($result2);
// if ($num2 > 0)
// header(&quot;location: AccessHomePage (user)/test1.htm&quot;)
{
$auth=&quot;yes&quot;;
$logname=$fusername;
$today = date(&quot;Y-m-d h:m:s&quot;);
$sql = &quot;INSERT INTO Login (ClientID,LoginTime)
VALUES ('$logname','$today')&quot;;
mysql_query($sql) or die(&quot;Can't execute query.&quot;);
}
// else
// {
// unset($do); // 44
//$message=&quot;The Login Name, '$fusername' exists, // 45
// but you have not entered the correct
// password! Please try again.<br>&quot;;
// }
// }
// elseif ($num == 0) // login name not found // 51
// {
// unset($do); // 53
// $message = &quot;The Login Name you entered does not
// exist! Please try again.<br>&quot;;
// }
//break; // 58


?>

<html>
<head>
<!-- TemplateBeginEditable name=&quot;doctitle&quot; -->
<title>Untitled Document</title>
<!-- TemplateEndEditable -->
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<!-- TemplateBeginEditable name=&quot;head&quot; -->
<!-- TemplateEndEditable -->
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function MM_displayStatusMsg(msgStr) { //v1.0
status=msgStr;
document.MM_returnValue = true;
}
//-->
</script>
</head>

<body>
<p><img src=&quot;Sites%20Title%20Border.png&quot; width=&quot;799&quot; height=&quot;89&quot;></p>
<p align=&quot;justify&quot;>Welcome to the new IT Help-Online web site. In order to process
your request quickly and efficiently could you please enter your details in
below. If you are a new user please click <a href=&quot;New User Details/NewUserDetails.php&quot; target=&quot;_top&quot;>here</a>.</p>
<p align=&quot;justify&quot;>&nbsp;</p>
<p align=&quot;justify&quot;>&nbsp;</p>
<form method=&quot;post&quot; name=&quot;form3&quot; onReset=&quot;MM_displayStatusMsg('Please enter your ClientID and Password');return document.MM_returnValue&quot;>

<table border=&quot;0&quot; align=&quot;center&quot;>
<?php // 23
if (isset($message))
echo &quot;<tr><td colspan='2'>$message </td></tr>&quot;;
?>

<tr>
<td align=right><b>ClientID</b></td>
<td><input name=&quot;ClientID&quot; type=&quot;text&quot; id=&quot;ClientID&quot; onFocus=&quot;MM_displayStatusMsg('Please enter your ClientID and Password');return document.MM_returnValue&quot; onSelect=&quot;MM_displayStatusMsg('Please enter your ClientID and Password');return document.MM_returnValue&quot; size=&quot;20&quot; maxlength=&quot;100&quot; maxsize=&quot;20&quot;>
</td>
</tr>
<tr>
<td width=&quot;120&quot; align=&quot;right&quot;><b>Password</b></td>
<td><input name=&quot;fpassword&quot; type=&quot;password&quot;
size=&quot;20&quot; maxlength=&quot;100&quot; maxsize=&quot; `20&quot;></td>
</tr>
<tr>
<td align=&quot;center&quot; colspan=&quot;2&quot;> <br> <input type=&quot;submit&quot; name=&quot;log&quot; value=&quot;Submit&quot;>
<input type=&quot;reset&quot; name=&quot;Reset&quot; value=&quot;Reset&quot;></td>
</tr>
</table>

</form>
<p align=&quot;justify&quot;>&nbsp;</p>
<p align=&quot;justify&quot;>&nbsp;</p>
<p align=&quot;justify&quot;>&nbsp;</p>
<p>Administrators log on <a href=&quot;Staff&Admin%20Login%20Page/Staff&Admin%20Login%20Page.php&quot;>here</a></p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>


This is the whole thing!
 
Hi, I'm back.

I going to put you an example I made to undestarnd -time ago- the sessions (no DB, sorry, but you can modify it).

Code:
1. ---------- login.html ------------------
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form method=post action=login.php>
User:
<input type=text name=user>
<br>
Password:
<input type=password name=pwd>
<br>
<input type=submit value=&quot;Login!&quot;>
</form>
</body>
</html>

2. ----------- login.php ------------------
<?php
	session_start();
	$user=$_POST[&quot;user&quot;];
	$pwd=$_POST[&quot;pwd&quot;];

	if (($user == &quot;Chacal&quot;) && ($pwd == &quot;admin&quot;))
	{
		$_SESSION['nombre']=&quot;Victor Cuevas D.&quot;;
		$_SESSION['logged']=&quot;Si&quot;;
		header(&quot;Location: index.php&quot;);
	} else
		header(&quot;Location: login.html&quot;);
?>

3. ------------ index.php ----------------
<?php
	session_start();
	
	include_once(&quot;control.php&quot;);

	if (!is_logged())
	{
		header(&quot;Location: login.html&quot;);
		exit(0);
	}
	echo &quot;Hola we'on..&quot; . $_SESSION[&quot;nombre&quot;] .&quot;<br>\n&quot;;
	echo &quot;<hr>\n&quot;;
	echo &quot;<a href='page2.php'>Vamos a la otra p&aacute;ginas</a><br>\n&quot;;
	echo &quot;<a href='salir.php'>A salgamos de esta we&aacute;</a><br>\n&quot;;
		
?>

4. ---------- control.php ---------------------
<?php
	function is_logged(){
		if ((!isset($_SESSION[&quot;logged&quot;])) || ($_SESSION[&quot;logged&quot;]!=&quot;Si&quot;))
			return FALSE;
		else 
			return TRUE;
	}
?>

5. ------------ page2.php ------------------
<?php
	session_start();
	
	include_once(&quot;control.php&quot;);
	
	if (!is_logged())
	{
		header(&quot;Location: login.html&quot;);
		exit(0);
	}
	echo &quot;Hola we'on..<br>\n&quot;;
	echo &quot;<hr>\n&quot;;
	echo &quot;<a href='index.php'>Vamos a la p&aacute;gina 1</a><br>\n&quot;;
	echo &quot;<a href='salir.php'>A salgamos de esta we&aacute;</a><br>\n&quot;;
		
?>

6. -------------- salir.php (get_out.php inenglish)----
<?php
	session_start();
	$_SESSION[]=array();
	session_destroy();
	header(&quot;Location: login.html&quot;);
?>

Try it and analyze it, if you have questions, let us know!

Cheers.
 
I see that you are using Dreamweaver to create some of the PHP code. Of course, I believe that hand coding is better since it teaches how it really works.
Using the canned DW code makes debugging this difficult and time intensive. We've discussed all the parts you need for the desired functionality and most of them are there even as written out code. I suggest that you solve the problem by:
1. Make a simple HTML form with the username and password fields that posts to the login script.
2. Write the login script in the way it was described in the previous posts.

The code you posted has also other issues. It is not a good idea to have filenames and URLs that contain spaces, ampersands etc. It's better to stick to contiguous filenames that stay away from special characters.
There are also references to HTTP_POST_VARS and HTTP_GET_VARS for the same variable 'ClientId'. That will mess things up also by making it ambiguos.

Best bet, again, write the thing from scratch and ditch the DW code. It will be to your benefit.
 
I agree with DRJ478, if you want to learn PHP forget DW and do all code hands-on!

When you have learned PHP you can use tools as DW, but if you don't know how PHP works, how can you fix problems made by DW?

Cheers.
 
Cheers for both your help, much appreciated! Gonna ditch DW MX for a bit I think! I am just trying your code Chacalinc and I am getting a parse error on line 19, 20?

$user=$_POST&quot;user&quot;;
$pwd=$_POST&quot;pwd&quot;;

any ideas? I think I am understanding this all a lot better now!
 
the problem is tek.tips.com display...

with no spaces, lines are:

$user=$_POST [ &quot;user&quot; ];
$pwd=$_POST [ &quot;pwd&quot; ];

remember, NO SPACES.
 
thanks! is the code meant to be on one page or split into different pages denoted by ---------- between each code bit?

and is the space problem the same for other lines like

echo &quot;Hola we'on..&quot; .$_SESSION&quot;nombre&quot;. &quot;<br>\n&quot;;

and line 49!
 
1. yes, differents file, denoted by ---name_file.ext --- in the same folder.

2. yes, line should be ... $_SESSION [ &quot;nombre&quot; ] ..., with no spaces.

be aware that lines with $_SESSIONS, $_POST and $_GET must be:

$_SOMETHING [ &quot;variable&quot; ] with no spaces.

Cheers.
 
how can you have a login.html and a login.php file in the same folder wont they over-write one another?
 
no, because login.html is a plain page and login.php has the scripting to let you get in or get you back to login.html.

Your apache config should have the line:

DirectoryIndex index.html index.htm index.php ...another indexes extensions..

whit this line you tell Apache to look for:
1. index.html, if it does not exist
2. index.htm, if it does not exist
3. index.php, if it does not exist
4, look for another indexes that match this directive.
 
Ok I have entered the code in, on seperate pages and loaded it up, I put the username in as Chacal and password admin but it does not come up with anything it just refreshes the login page. I think all the code is in correctly as when I load each page individually no errors appear! any ideas?
 
1. in &quot;control.php&quot; the line

if ((!isset($_SESSION&quot;logged&quot;)) || ($_SESSION&quot;logged&quot;!=&quot;Si&quot;))

do you have $_SESSION [ &quot;logged&quot; ] with no spaces?

try replacing in the same line:
$_SESSION['logged'] instead of $_SESSION [ &quot;logged&quot; ] (note the difference is using ' instead of &quot; ).

2. what browser are you using?
 
I am using IE 6.0, its still not working!
 
just to make sure the sessions vars, in index.php on lines:

if (!is_logged())
{
header(&quot;Location: login.html&quot;);
exit(0);
}

let them as comentaries:
/* if (!is_logged())
{
header(&quot;Location: login.html&quot;);
exit(0);
}
*/
and try login.html

when index.php displays, a name should be written (Victor...)

What is your server? w2k, linux?
are you running apache?
Could you try with IE55 or mozilla/netscape?
 
Yeah that worked! it came up with the second page and had a couple of links on them! I presume that is correct! its just that part that was commented out, is this needed and what does it do?
 
oh.. don't harry up.

It was only a test, with commented lines you forget the secutiry, I mean if you go to index.php directly (not from login.html) you will have access. If you click on link to page2.php you could have the same problem.

1. The name &quot;Victor...&quot; is displayed?
2. could you try, uncommenting the lines, from IE5 and mozilla/netscape?

I don't know why some IE explorers have problems with session... actually, this pages (you are using right now) are working good in my apache server and I tested them with mozilla, netscape, IE5, 55 and IE6 with no problem.

When I make pages with sessions for login enabled pages, sometime I got the same problem, and then they are resolved, but I still don't know why..

plz, try with mozilla or another browser (even IE5), uncommenting the lines.
 
BTW, as you can see, I speak spanish (native language), sorry if my english is not quit good. :o)

on index.php you have 2 links:

echo &quot;<a href='page2.php'>Vamos a la otra páginas</a><br>\n&quot;;
echo &quot;<a href='salir.php'>A salgamos de esta weá</a><br>\n&quot;;

page2.php is secured, before it is displayed it will see whether you are loged in or not (same as index.php)

salir.php in english is getout.php, it means, kill sessions variables and sends you to login.html.

Try them!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top