dhrups Posted May 3, 2006 Share Posted May 3, 2006 I am trying to use sessions but it doesnt seem to work. Can someone please help me.[b]file1: scs_admin.php[/b]<html><head><title>Administrator Page</title></head><body><?phprequire_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>---------------------------------------[u][b]File 2 : add_page.php[/b][/u]<?phprequire_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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 3, 2006 Share Posted May 3, 2006 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:[code]<?phpsession_start()?>[/code]before the "<html>" tag.Ken Quote Link to comment Share on other sites More sharing options...
puja Posted May 3, 2006 Share Posted May 3, 2006 hey dhrupsu alrite? lolif 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:[code]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 {}[/code]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 helpsif it dont let me know Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 3, 2006 Share Posted May 3, 2006 Change this the top part of scs_admin.php:[code]<html><head><title>Administrator Page</title></head><body><?phprequire_once("config.php");?><?session_start();[/code]to the following:[code]<?phpsession_start();?><html><head><title>Administrator Page</title></head><body><?phprequire_once("config.php");[/code]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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.