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!

Using an SQL statement and a starting a SESSION together

Status
Not open for further replies.

Sitehelp

Technical User
Joined
Feb 4, 2004
Messages
142
Location
GB
Hello all! ok I have a quick question, on my login page I have a session that opens so that the users details can be opened on the next few pages. The username and password box are in the same form and the session, as its supposed to, is declared at the top of the code b4 the form iteself, however, when I have the form as:

<form action = &quot; Logged In/WelcomeUserPage.php&quot;>


It works great but it does not check the username and password using the sql at the top of the code. When I have the form as:

<form method=&quot;post&quot; onReset=&quot;MM_displayStatusMsg('Please enter your ClientID and Password');return document.MM_returnValue&quot;>

The SQL at the top of the page cross checks the user in the DB and logs them in if the fields are correct but does not implement the session and therefore I cannot get the users details on the other pages. Has anyone any idea how I could use the username and password authentication check (the SQL) and start the session as well. Just in case you need to see the SQL, it is:

session_start(); ?>
<?php
$sql = &quot;SELECT ClientID, cpassword FROM clientinfo
WHERE ClientID='$ClientID'
AND cpassword='$Password'&quot;;

$result = mysql_query($sql)
or die(&quot;Error: MySQL said &quot;.mysql_error());
$num = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);

if ($num == 1)
header(&quot;location: Logged In/WelcomeUserPage.php?&quot;);
?>
//THE CODE FOR THE FORM ETC.... COMES NEXT!!!!!!!!!!!!!!!!


Does any one have any ideas, its driving me crazy. Cheers for the help.
 
Sorry! Now it is checking the password and ClientID to the one in the DB and is letting them through if they match. When they do match though it passes through but doesnt appear to pass the information through in the session, this has been my problem all along, it doesnt pass the ClientID over which is what I need it to do in order to bring up information related to that client. Thanks!
 
Is there any reason why the clientID is not copying across pages even though I am using a session in my script?
 
In all the code you've ever posted, you have not set a session variable named &quot;ClientID&quot;. You have to set them to have them.

Somewhere in your code, you have to perform something like:

$_SESSION['ClientID'] = <some value>;

Want the best answers? Ask the best questions: TANSTAAFL!!
 
What if the <some Value> is the value that the user enters, how do I get it to post that, I see what you mean it needs a value to post, the user enters this ClientID as well as their password and I want the string they enter to be what passes across the pages.
 
There does the value currently reside? Inside the array $_POST?

In such case, you need to set a session variable to hold the value of one element of $_POST. Something like:

$_SESSION['ClientID'] = $_POST['ClientID'];

would probably be appropriate.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Where would I place this, below the start_session but before the SQL?
 
I decided to try cookies instead as I wanted to see if this would work. I created a blank page and entered just:

setcookie(&quot;test&quot;, &quot;test&quot;);

Then I loaded it up in the browser and in the URL typed:

Javascript:alert(document.cookie);

The alert box told me that a cookie called &quot;test&quot; with the variable &quot;test&quot; had been stored. I then decided to include this in my code instead of sessions and typed:

mysql_select_db($database_MARTIN, $MARTIN);
if (isset($_POST['ClientID']) and isset($_POST['Password']))

{
$sql = &quot;SELECT ClientID, cpassword, CDept FROM clientinfo WHERE ClientID='$ClientID' AND cpassword='$Password' AND Cdept='$CDept'&quot;;

$result = mysql_query($sql) or die(&quot;Error: MySQL said &quot;.mysql_error());

$num = mysql_num_rows($result);
$row = mysql_fetch_assoc($result);

if ($num != 0)
{
setcookie (&quot;Client_ID&quot;, $_POST['$ClientID']);
header(&quot;location: Logged In/WelcomeUserPage.php?&quot;);
}
else
{
output_form ('Your login was unsuccessful - Please check your details and try again', $_POST['ClientID']);
}
}
else
{
output_form();
}
?>

But it still wont work, I tried to call the cookie up in the next page by typing:

<?php print($_cookie[&quot;Client_ID&quot;]); ?>

Nothing! any ideas on this as nothing seems to go through on these pages????
 
thanks! At last we are getting close I think as when I create a completely blank page and call it &quot;test&quot; then put in the script for that page JUST:

<?php print($_COOKIE[&quot;Client_ID&quot;]); ?>

The page brings up the ClientID. When I try to do this for my other original page, the one intended to be shown when the user logs on, it shows nothing. It must be this page then as it contains borders and buttons, code of which I had to do in Dreamweaver as I wasnt sure how to do it myself. I think this is the problem the second page, however I cannot locate the problem within it, the page looks like (sorry about the mess of &quot;welcomeuserpage.php&quot; its because of the images etc...):

<?php print($_cookie[&quot;Client_ID&quot;]); ?>
<html>
<head>
<!-- TemplateBeginEditable name=&quot;doctitle&quot; -->
<title>HelpOnlineTemplate - Admin.gif</title>
<!-- TemplateEndEditable -->
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;&quot;>
<!-- Fireworks MX Dreamweaver MX target. Created Wed Jan 21 18:10:42 GMT+0000 (GMT Standard Time) 2004-->
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName==&quot;Netscape&quot;)&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a.indexOf(&quot;#&quot;)!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
var i,img,nbArr,args=MM_nbGroup.arguments;
if (event == &quot;init&quot; && args.length > 2) {
if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
nbArr[nbArr.length] = img;
for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args)) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = args[i+1];
nbArr[nbArr.length] = img;
} }
} else if (event == &quot;over&quot;) {
document.MM_nbOver = nbArr = new Array();
for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args)) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
nbArr[nbArr.length] = img;
}
} else if (event == &quot;out&quot; ) {
for (i=0; i < document.MM_nbOver.length; i++) {
img = document.MM_nbOver; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
} else if (event == &quot;down&quot;) {
nbArr = document[grpName];
if (nbArr)
for (i=0; i < nbArr.length; i++) { img=nbArr; img.src = img.MM_up; img.MM_dn = 0; }
document[grpName] = nbArr = new Array();
for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args)) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
nbArr[nbArr.length] = img;
} }
}
//-->
</script>
<!-- TemplateBeginEditable name=&quot;head&quot; --><!-- TemplateEndEditable -->
</head>
<body bgcolor=&quot;#ffffff&quot; onLoad=&quot;MM_preloadImages('../Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button3.gif','../Templates/Admin%20Template/Buttons3(User)/Users%20Highlighted%20Button.gif','../Templates/Admin%20Template/Buttons3(User)/Additional%20Highlighted%20Button3.gif','../Templates/Admin%20Template/Buttons3(User)/Log%20Out%20Highlighted%20Button3.gif','Calls/Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button.gif','Calls/Templates/Admin%20Template/Buttons3(User)/Users%20Highlighted%20Button.gif','Calls/Templates/Admin%20Template/Buttons3(User)/Log%20Out%20Highlighted%20Button3.gif','../Templates/Admin%20Template/Buttons3(User)/Highlighted%20Home%20Button3.gif','../Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button.gif')&quot;>
<div id=&quot;Layer1&quot; style=&quot;position:absolute; left:23px; top:173px; width:102px; height:152px; z-index:1&quot;><!-- #BeginLibraryItem &quot;/Library/Users%20Buttons.lbi&quot; -->
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a.indexOf(&quot;#&quot;)!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_nbGroup(event, grpName) { //v6.0
var i,img,nbArr,args=MM_nbGroup.arguments;
if (event == &quot;init&quot; && args.length > 2) {
if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
nbArr[nbArr.length] = img;
for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args)) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = args[i+1];
nbArr[nbArr.length] = img;
} }
} else if (event == &quot;over&quot;) {
document.MM_nbOver = nbArr = new Array();
for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args)) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
nbArr[nbArr.length] = img;
}
} else if (event == &quot;out&quot; ) {
for (i=0; i < document.MM_nbOver.length; i++) {
img = document.MM_nbOver; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
} else if (event == &quot;down&quot;) {
nbArr = document[grpName];
if (nbArr)
for (i=0; i < nbArr.length; i++) { img=nbArr; img.src = img.MM_up; img.MM_dn = 0; }
document[grpName] = nbArr = new Array();
for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args)) != null) {
if (!img.MM_up) img.MM_up = img.src;
img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
nbArr[nbArr.length] = img;
} }
}
//-->
</script>
<body onLoad=&quot;MM_preloadImages('../Project/Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button3.gif','../Project/Templates/Admin%20Template/Buttons3(User)/Users%20Highlighted%20Button.gif','../Project/Templates/Admin%20Template/Buttons3(User)/Additional%20Highlighted%20Button3.gif','../Project/Templates/Admin%20Template/Buttons3(User)/Log%20Out%20Highlighted%20Button3.gif','../Project/User%20Logged%20In/Calls/Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button.gif','../Project/User%20Logged%20In/Calls/Templates/Admin%20Template/Buttons3(User)/Users%20Highlighted%20Button.gif','../Project/User%20Logged%20In/Calls/Templates/Admin%20Template/Buttons3(User)/Log%20Out%20Highlighted%20Button3.gif','../Project/Templates/Admin%20Template/Buttons3(User)/Highlighted%20Home%20Button3.gif','../Project/Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button.gif')&quot;>
<!-- #BeginLibraryItem &quot;/Library/Users%20Buttons.lbi&quot; --><table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr>
<td><a href=&quot;WelcomeUserPage.php&quot; target=&quot;_top&quot; onClick=&quot;MM_nbGroup('down','group1','Home','',1)&quot; onMouseOver=&quot;MM_nbGroup('over','Home','../Templates/Admin%20Template/Buttons3(User)/Highlighted%20Home%20Button3.gif','',1)&quot; onMouseOut=&quot;MM_nbGroup('out')&quot;><img src=&quot;../Templates/Admin%20Template/Buttons3(User)/Home%20Button3.gif&quot; alt=&quot;Back to Home Page&quot; name=&quot;Home&quot; width=&quot;95&quot; height=&quot;26&quot; border=&quot;0&quot; onload=&quot;&quot;></a></td></tr>
<tr>
<td><a href=&quot;Calls/ViewCallOptions.php&quot; target=&quot;_top&quot; onClick=&quot;MM_nbGroup('down','group1','CallDetails','',1)&quot; onMouseOver=&quot;MM_nbGroup('over','CallDetails','../Templates/Admin%20Template/Buttons3(User)/Call%20Highlighted%20Button.gif','',1)&quot; onMouseOut=&quot;MM_nbGroup('out')&quot;><img src=&quot;../Templates/Admin%20Template/Buttons3(User)/Call%20Button.gif&quot; alt=&quot;View Call Options&quot; name=&quot;CallDetails&quot; width=&quot;95&quot; height=&quot;26&quot; border=&quot;0&quot; onload=&quot;&quot;></a></td></tr>
<tr>
<td><a href=&quot;Users/ModifyDetails.php&quot; target=&quot;_top&quot; onClick=&quot;MM_nbGroup('down','group1','UserDetails','',1)&quot; onMouseOver=&quot;MM_nbGroup('over','UserDetails','../Templates/Admin%20Template/Buttons3(User)/Users%20Highlighted%20Button.gif','',1)&quot; onMouseOut=&quot;MM_nbGroup('out')&quot;><img src=&quot;../Templates/Admin%20Template/Buttons3(User)/User%20Button.gif&quot; alt=&quot;Modify you user details&quot; name=&quot;UserDetails&quot; width=&quot;95&quot; height=&quot;26&quot; border=&quot;0&quot; onload=&quot;&quot;></a></td></tr>
<tr>
<td><a href=&quot;LogOut.php&quot; target=&quot;_top&quot; onClick=&quot;MM_nbGroup('down','group1','LogOut','',1)&quot; onMouseOver=&quot;MM_nbGroup('over','LogOut','../Templates/Admin%20Template/Buttons3(User)/Log%20Out%20Highlighted%20Button3.gif','',1)&quot; onMouseOut=&quot;MM_nbGroup('out')&quot;><img src=&quot;../Templates/Admin%20Template/Buttons3(User)/Log%20Out%20Button.gif&quot; alt=&quot;Log Out&quot; name=&quot;LogOut&quot; width=&quot;95&quot; height=&quot;26&quot; border=&quot;0&quot; onload=&quot;&quot;></a></td></tr>
</table><!-- #EndLibraryItem --><!-- #EndLibraryItem --></div>
<table bgcolor=&quot;#ffffff&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;799&quot;>
<!-- fwtable fwsrc=&quot;HelpOnlineTemplate.png&quot; fwbase=&quot;HelpOnlineTemplate - Admin.gif&quot; fwstyle=&quot;Dreamweaver&quot; fwdocid = &quot;742308039&quot; fwnested=&quot;0&quot; -->
<tr>
<td>&nbsp;</td>
<td><img src=&quot;../Templates/Admin%20Template/spacer.gif&quot; width=&quot;678&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot;></td>
<td><img src=&quot;../Templates/Admin%20Template/spacer.gif&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot;></td>
</tr>

<tr>
<td colspan=&quot;2&quot;><img name=&quot;HelpOnlineTemplate2020Admin_r1_c1&quot; src=&quot;../Templates/Admin%20Template/HelpOnlineTemplate%20-%20Admin_r1_c1.gif&quot; width=&quot;799&quot; height=&quot;89&quot; border=&quot;0&quot; alt=&quot;&quot;></td>
<td><img src=&quot;../Templates/Admin%20Template/spacer.gif&quot; width=&quot;1&quot; height=&quot;89&quot; border=&quot;0&quot; alt=&quot;&quot;></td>
</tr>
<tr>
<td><img name=&quot;HelpOnlineTemplate2020Admin_r2_c1&quot; src=&quot;../Templates/Admin%20Template/HelpOnlineTemplate%20-%20Admin_r2_c1.gif&quot; width=&quot;121&quot; height=&quot;510&quot; border=&quot;0&quot; alt=&quot;&quot;></td>
<td valign=&quot;top&quot; bgcolor=&quot;#ffffff&quot;><!-- TemplateBeginEditable name=&quot;MainCenter&quot; -->
<p>&nbsp;</p>
<!-- TemplateEndEditable --></td>
<td><img src=&quot;../Templates/Admin%20Template/spacer.gif&quot; width=&quot;1&quot; height=&quot;510&quot; border=&quot;0&quot; alt=&quot;&quot;></td>
</tr>
</table>
</body>
</html>


The difference between this page and the test one is the borders thats it, the only script is the top line which works in test but not in welcomeuserpage.php.
 
Whenever you post code in Tek-Tips, put

[ignore]
Code:
...
[/ignore]

tags around it, or uncheck the &quot;Process TGML&quot; checkbox.

This stops the TGML processor from mistaking an array subscript i from being interpreted as the tag to begin italics.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
I would like to thank sleipnir214 for helping me out with this problem! I have now got it working, at last! I cleaned the pages up and found that the pages are now passing the ClientIDs accross one another and it is also logging them in correctly, however it wouldnt have worked without your valuable input! Anyway thanks for all the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top