Jump to content


Photo

sessions


  • Please log in to reply
3 replies to this topic

#1 dhrups

dhrups
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 May 2006 - 06:19 PM

I am trying to use sessions but it doesnt seem to work. Can someone please help me.

file1: scs_admin.php

<html>

<head>
<title>Administrator Page</title>
</head>

<body>

<?php

require_once("config.php");

?>

<?

session_start();

$u_user_id = $_POST["user_id"];
$u_password = $_POST["password"];


$query = "SELECT * FROM users WHERE user_id='$u_user_id' AND password='$u_password'";
$result = mysql_query($query, $connection) or die(mysql_error());
if(mysql_num_rows($result)=="1") {
$_SESSION[user_id]=$_POST[user_id] && $_SESSION[password]=$_POST[password];


?>

<table border="0" width="100%" height="1">
<tr>
<td width="15%" rowspan="3" height="1" bgcolor="#3399FF" valign="top">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" id="AutoNumber2">
<tr>
<td width="100%">
<p align="center"><font color="#FFFFFF"><b><a href="home.php">
<font color="#FFFFFF">HOME</font></a></b></font></td>
</tr>
</table>
<p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" id="AutoNumber3">
<tr>
<td width="100%">
<p align="center"><font color="#FFFFFF"><b><a href="logout.php">
<font color="#FFFFFF">LOG OUT</font></a></b></font></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="85%" height="1" bgcolor="#66CCFF">
<h3 align="center"><font size="6" color="#FFFFFF">
<span style="background-color: #0000FF">INTERCROSS AGENCIES LTD</span></font></h3>
<h3 align="center"><span style="background-color: #00FF00">STOCK CONTROL
SYSTEM</span></h3>
<hr size="1" color="#000000" noshade>
</td>
</tr>
<tr>
<td width="85%" height="10" bgcolor="#C0C0C0"><b> </b><p align="center">
<font color="#FF0000"><b><u><font size="4">ADMINISTRATOR PAGE</font></u></b></font><font size="4">
</font></p>
<p> </p>
<p><b>PLEASE SELECT THE APPROPRIATE OPTIONS:</b></p>
<form method="POST" action="scs_admin.php">
<table border="3" cellpadding="0" cellspacing="0" style="border-style:double; border-width:3; border-collapse: collapse; " bordercolor="#111111" width="100%" id="AutoNumber1" height="157">
<tr>
<td width="21%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="43" align="center">
<p align="center">
<a href="create_user.php">
<input type="button" value="CREATE USER" name="B3" style="font-weight: bold"></a></td>
<td width="20%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="43" align="center">
<p align="center"><a href="scs_delete_user.php">
<input type="button" value="DELETE USER" name="B7" style="color: #FF0000; font-weight: bold"></a></td>
<td width="34%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="43" colspan="2" align="center">
<p align="center"><a href="search_models.php">
<input type="button" value="SEARCH MACHINES" name="B9" style="font-weight: bold"></a></td>
<td width="25%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="43" colspan="2" align="center">
<p align="center"><a href="scs_views.php">
<input type="button" value="SEARCH SPARES" name="B10" style="font-weight: bold"></a></td>
</tr>
<tr>
<td width="41%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="35" colspan="2" align="center">
<p align="center">
</td>
<td width="59%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="35" colspan="4" align="center"> </td>
</tr>
<tr>
<td width="21%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="39" align="center">
<p align="center">
<a href="stock_page.php">
<input type="button" value="STOCK LEVELS" name="B4" style="color: #0000FF; font-weight: bold"></a></td>
<td width="20%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="39" align="center">
<p align="center">
<a href="create_report.php">
<input type="button" value="REPORTS" name="B8" style="color: #0000FF; font-weight: bold"></a></td>
<td width="20%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="39" align="center">
<a href="add_machines.php">
<input type="button" value="ADD MACHINES" name="B11" style="font-weight: bold"></a></td>
<td width="20%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="39" align="center" colspan="2">
<a href="add_spares.php">
<input type="button" value="ADD SPARES" name="B12" style="font-weight: bold"></a></td>
<td width="19%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="39" align="center">
<a href="add_accessories.php">
<input type="button" value="ADD ACCESSORIES" name="B13" style="font-weight: bold"></a></td>
</tr>
<tr>
<td width="41%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="32" colspan="2" align="center">
</td>
<td width="59%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="32" align="center" colspan="4">
</td>
</tr>
<tr>
<td width="41%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="32" colspan="2" align="center">
<p align="center"> </td>
<td width="59%" style="border-style: double; border-width: 3; ; padding-left:4; padding-right:4; padding-top:1; padding-bottom:1" height="32" align="center" colspan="4">
<p><a href="delete_stock.php">
<input type="button" value="DELETE STOCK" name="B14" style="font-weight: bold"></a></td>
</tr>
</table>
<p> </p>
</form>
<p> </p>
<p> </p>
<p></p>
<hr size="1" color="#000000" noshade>
</td>
</tr>
</table>

<?

}
else{

?>

<table border="0" width="100%" height="1">
<tr>
<td width="13%" rowspan="2" height="1" bgcolor="#3399FF" valign="top">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" id="AutoNumber4">
<tr>
<td width="100%">
<p align="center"><font color="#FFFFFF"><b><a href="home.php">
<font color="#FFFFFF">HOME</font></a></b></font></td>
</tr>
</table>
<p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#FFFFFF" width="100%" id="AutoNumber5">
<tr>
<td width="100%">
<p align="center"><font color="#FFFFFF"><b><a href="logout.php">
<font color="#FFFFFF">LOG OUT</font></a></b></font></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="87%" height="1" bgcolor="#C0C0C0">
<h3 align="center"><span style="background-color: #00FF00">LOG IN FAILED!!!!!!</span></h3>
<p align="center">Wrong User Id or Password supplied.</p>
<p align="center">Please Try Again!<p align="center">

<a href="admin_login.php"><span style="background-color: #FFFF00">
<font color="#000000">Administrator Log In</font></span></a></tr>
</table>

<?

}

?>

</body>
</html>

---------------------------------------
File 2 : add_page.php


<?php

require_once("config.php");

session_start();

if(!isset($_SESSION[user_id])) { echo "<p><center>Please login to view this page.</p><p> </p> Please wait... You will automatically be redirected back to the login page";
echo "<META HTTP-EQUIV=Refresh CONTENT='3; URL=home.php'>";

} else {
}

?>
<html>

<head>
<title>Staff Page</title>
</head>

<body>



<table border="0" width="100%" height="1">
<tr>
<td width="15%" rowspan="3" height="1" bgcolor="#3399FF" valign="top">
<table border="1" cellspacing="1" width="100%" id="AutoNumber3">
<tr>
<td width="100%" align="center">
<b><font color="#FFFFFF"><a href="home.php">
<font color="#FFFFFF">HOME</font></a></font></b></td>
</tr>
<tr>
<td width="100%" align="center">
<font color="#FFFFFF"><b><a href="scs_staff.php">
<font color="#FFFFFF">BACK</font></a></b></font></td>
</tr>
<tr>
<td width="100%">
<p align="center"> </td>
</tr>
</table>
<p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center"> <p align="center">
<p align="center">
<table border="1" cellspacing="1" width="100%" id="AutoNumber2">
<tr>
<td width="100%">
<p align="center"><b><font color="#FFFFFF"><a href="logout.php">
<font color="#FFFFFF">LOG OUT</font></a></font></b></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="85%" height="1" bgcolor="#66CCFF">
<h3 align="center"><font size="6" color="#FFFFFF">
<span style="background-color: #0000FF">INTERCROSS AGENCIES LTD</span></font></h3>
<h3 align="center"><span style="background-color: #00FF00">STOCK CONTROL
SYSTEM</span></h3>
<hr size="1" color="#000000" noshade>
</td>
</tr>
<tr>
<td width="85%" height="10" bgcolor="#C0C0C0"><b> </b><p align="center">
<u><font size="4" color="#FF0000"><b>ADD PAGE</b></font></u></p>
<p><b>SELECT OPTIONS:</b></p>
<form method="POST" action="scs_staff.php">
<table border="1" cellspacing="1" width="100%" id="AutoNumber1" height="53">
<tr>
<td width="25%" height="48" align="center">
<a href="add_machines.php">
<input type="button" value="ADD MACHINES" name="B1" style="font-weight: bold"></a></td>
<td width="25%" height="48" align="center">
<a href="add_spares.php">
<input type="button" value="ADD SPARES" name="B2" style="font-weight: bold"></a></td>
<td width="25%" height="48" align="center">
<a href="add_accessories.php">
<input type="button" value="ADD ACCESSORIES" name="B3" style="font-weight: bold"></a></td>

</tr>
</table>
<p> </p>
<p> </p>
</form>
<p> </p>
<p></p>
<hr size="1" color="#000000" noshade>
</td>
</tr>
</table>

</body>
</html>

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 May 2006 - 06:31 PM

The session_start() function call must be executed before anything is sent to the screen. Most people put it at the very beginning of the file.

In your case in scs_admin.php put:
<?php
session_start()
?>
before the "<html>" tag.

Ken

#3 puja

puja
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts

Posted 03 May 2006 - 06:31 PM

hey dhrups
u alrite? lol

if u use the session stuff on ur login page does the login still work?

the thing that is wrong with ur last script is that with this part:

session_start();

if(!isset($_SESSION[user_id])) { echo "<p><center>Please login to view this page.</p><p> </p> Please wait... You will automatically be redirected back to the login page";
echo "<META HTTP-EQUIV=Refresh CONTENT='3; URL=home.php'>";

} else {
}


u need to close the else after ur script so at the end of ur page coz ur saying that if the session isnt set then redirect them to the login page otherwise u let them into the script.
lol hope that helps
if it dont let me know

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 May 2006 - 06:41 PM

Change this the top part of scs_admin.php:
<html>

<head>
<title>Administrator Page</title>
</head>

<body>

<?php

require_once("config.php");

?>

<?

session_start();
to the following:
<?php
session_start();
?>
<html>

<head>
<title>Administrator Page</title>
</head>

<body>

<?php
require_once("config.php");

Always place session_start as the first line of code in any page that requires sessions. Do not output HTML before the use of session_start otherwise your sessions will fail to work.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users