473,396 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Session Problem

I'm trying to use sessions to allow people to log into my site. The problem
is that sometimes it works and sometimes it doesn't! As far as I'm aware my
script is fine (hence why it works sometimes) and I've had people tell me
it's fine too. Basically, when a user enters their username and password it
checks that against my mysql database and if correct creates a session and
stores their username in it in a variable called uname. Then, when they
access another area of the site it checks to see it uname is set and if it
is then it lets them access that area and if it isn't asks them to log in
again. The session id is kept so I know the session is there but it doesn't
always register the variable uname. I think the php version is 4.1.2 (it's
hosted on sourceforge).

Can anyone help?

Darrell

Jul 16 '05 #1
2 2558
Darrell,

Can you post some of your code? Otherwise we can only guess as to why
it isn't working, and even that would probably be a long shot.

Marcus

Jul 16 '05 #2
Ok. I've listed three files below. admin.php is where the user enters their
username and password. admin2.php is where it checks the username and
password and displays the admin section and addnews.php allows you to add
news to the site. It's when I try to add news when it sometimes works and
sometimes doesn't. When I click add news half of the time it shows the add
news section and half of the time it doesn't. This is because uname isn't
set when it should be (and is half of the time). If you want to see the site
go to http://linrar.sourceforge.net/index2.php (I will rename it index.php
once the site works).

admin.php

<?php
session_start();
session_unset();
session_destroy();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>LinRAR Homepage</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td width="122" valign="top">
<table width="122" border="0" cellpadding="2" cellspacing="0"
bgcolor="#000000">
<tr>
<td>
<table width="120" border="0" cellpadding="5" cellspacing="0"
bgcolor="#ffffff">
<tr>
<td>
<a href=index.php>News</a><br>
<a href=about.php>About</a><br>
<a href=download.php>Download</a><br>
<a href=support.php>Support</a><br>
<a href="http://sourceforge.net/projects/linrar">Sourceforge</a><br>
<a href="admin.php">Admin</a><br>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td valign="top">
<h1>LinRAR - Admin</h1>
<br>
To log in to the admin section type in your username and password below and
click enter:<br><br>
<form action=admin2.php method=post>
<table>
<tr><td>
UserName</td><td><input type="text" name="lruname"></td></tr>
<tr><td>
Password</td><td><input type="password" name="lrpass"></td></tr>
</table>
<br>
<input type="submit" value="Enter" class="flatbutton">
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

admin2.php

<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>LinRAR Homepage</title>
</head>
<body>
<?php
$notfounduser = 0;
$passwordincorrect = 0;
$db = "linrar";
$user = "linrar";
$pass = "linrardb";
$link = mysql_connect("mysql.sourceforge.net", $user, $pass);
mysql_select_db($db, $link)
or die("Couldn't open $db: ".mysql_error());
$result = mysql_query("SELECT * from users");
$num_rows = mysql_num_rows($result);
while ($a_row = mysql_fetch_array($result))
{
if ($a_row[lruname] != $lruname)
$notfounduser = 1;
else
{
$notfounduser = 0;
$lrpass = md5($lrpass);
if ($a_row[lrpass] != $lrpass)
$passwordincorrect = 1;
}
if ($notfounduser == 0)
break;
}

if ($notfounduser == 1)
print "User $lruname was not found.";
if ($passwordincorrect == 1)
print "Incorrect password entered for user $lruname.";

if ($notfounduser == 0 && $passwordincorrect == 0)
{
session_register("uname");
$uname = $lruname;
print "<table border=0 cellpadding=0 cellspacing=0>";
print "<tr>";
print "<td>";
print "<table border=0 cellpadding=10 cellspacing=0>";
print "<tr>";
print "<td width=122 valign=top>";
print "<table width=122 border=0 cellpadding=2 cellspacing=0
bgcolor=#000000>";
print "<tr>";
print "<td>";
print "<table width=120 border=0 cellpadding=5 cellspacing=0
bgcolor=#ffffff>";
print "<tr>";
print "<td>";
print "<a href=\"index.php\">Back (Logout)</a><br>";
print "<a href=\"addnews.php?".session_name()."=".session_id ()."\">Add
News</a><br>";
print "<a href=\"addadmin.php?".session_name()."=".session_i d()."\">Add
Admin</a><br>";
print "<a
href=\"changepwd.php?".session_name()."=".session_ id()."\">Change
Password</a><br>";
print "<a href=BoardPlus/forum.html>Forum</a><br>";
print "<a href=\"phpMyAdmin-2.5.1\"</a>phpMyAdmin<br>";
print "<a href=count.php>Site Stats</a><br>";
print "</td>";
print "</tr>";
print "</table>";
print "</td>";
print "</tr>";
print "</table>";
print "</td>";
print "<td valign=top>";
print "<h1>LinRAR - Admin</h1>";
print "<br>";
print "Welcome $uname to the admin section.";
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

addnews.php

<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>LinRAR Homepage</title>
</head>
<body>
<?php
if (session_is_registered("uname"))
{
print "<table border=0 cellpadding=0 cellspacing=0>";
print "<tr>";
print "<td>";
print "<table border=0 cellpadding=10 cellspacing=0>";
print "<tr>";
print "<td width=122 valign=top>";
print "<table width=122 border=0 cellpadding=2 cellspacing=0
bgcolor=#000000>";
print "<tr>";
print "<td>";
print "<table width=120 border=0 cellpadding=5 cellspacing=0
bgcolor=#ffffff>";
print "<tr>";
print "<td>";
print "<a href=\"index.php\">Back (Logout)</a><br>";
print "<a href=\"addnews.php?".session_name()."=".session_id ()."\">Add
News</a><br>";
print "<a href=\"addadmin.php?".session_name()."=".session_i d()."\">Add
Admin</a><br>";
print "<a href=\"changepwd.php?".session_name()."=".session_ id()."\">Change
Password</a><br>";
print "<a href=BoardPlus/forum.html>Forum</a><br>";
print "<a href=\"phpMyAdmin-2.5.1\"</a>phpMyAdmin<br>";
print "<a href=count.php>Site Stats</a><br>";
print "</td>";
print "</tr>";
print "</table>";
print "</td>";
print "</tr>";
print "</table>";
print "</td>";
print "<td valign=top>";
print "<h1>LinRAR - Add News</h1>";
print "<br>";

if ($changed == 1)
{
$dateadded = time();
$db = "linrar";
$user = "linrar";
$pass = "linrardb";
$link = mysql_connect("mysql.sourceforge.net", $user, $pass);

if (!$link)
die ("Couldn't connect to MySQL: ".mysql_error());
mysql_select_db($db)
or die("Couldn't connect to database $db: ".mysql_error());

$query = "INSERT INTO news VALUES ('$uname', '$dateadded', '$ntitle',
'$nmessage')";

mysql_query($query, $link)
or die ("Couldn't add data to the table: ".mysql_error());

mysql_close($link);
print "The following news has been added...<br><br>";
print "Title: $ntitle<br><br>";
print "Message: $nmessage<br><br>";
print "Posted by: $uname<br><br>";
$dateadded = date("d/m/y", $dateadded);
print "Date added: $dateadded<br>";
}
else
{
print "<form action=addnews.php?$sess&changed=1 method=post>";
print "Title <input type=text size=50 name=ntitle><br><br>";
print "Message<br>";
print "<textarea rows=5 cols=70 name=nmessage></textarea><br><br>";
print "<input type=submit value=Add>";
print "</form>";
}
}
else
{
print "Not logged in. <a href=admin.php>Click Here</a> to log in.";
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

Jul 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jonathan Chong | last post by:
I have problem with AOL browser (IE and Netscape are OK) accessing my Web site after putting up a load balancer that will go to W1 or W2. The problem does not happen when there is only Web server...
5
by: Roman | last post by:
Hi, I've installed .net on my server and since then I'm loosing my sessions from my (old) asp. e.g.: Session("test") works on the same page, but if the page is changed the whole session is...
0
by: Marek | last post by:
Hi, I have the following session problem: I've created simple login/logout form to prevent unknown users from accessing my private web site part and It is not working on the www server. It seems...
0
by: Sabari | last post by:
Hi, In our application we face a session problem.Its kinda strange as well.We have our session timeout in web.config file to 240 minutes.It works fine in our intranet.When we moved the project to...
1
by: caldera | last post by:
Hello, We have a session problem. A dataset is put into a session and call this session value later. When we run this web page in http://localhost it works. But server has a specific name like...
4
by: alan | last post by:
Hi, i have some problem in asp.net session problem in page_load -> i have Label1.text = Session("a") in DataGrid1_sortCommand -> i have Session("a") = e.sortExpression
3
by: sri | last post by:
Hi, I have Login page (login.aspx") and after entering into inbox by giving valid userid and password the session item is userid and password. In inbox page ("inbox.aspx") i have logoff button and...
2
by: Tom | last post by:
I hope someone can help me figure out what's going on here. I've re-read the section on sessions at php.net and Googled this high and low but I haven't found anything that quite explains my...
3
by: deepsfriend4u | last post by:
Hello, i have one application in that i use session . problem is, same code and session data use work successful in Firefox but not work in Internet Explore. Thanks & Regards ...
2
by: Kong Chun Ho | last post by:
Hi all, I have a php session problem when i echo a session_id, it return nothing! <?php if(!session_start()) { exit(); }else{ ob_start(); session_start(); echo session_id(); echo SID;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.