473,699 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP ajax session does not work

3 New Member
hello, all:

I am having trouble to make ajax work for my tiny php app.

I have a index.php file, which is
[php]<?PHP
session_start() ;
$_SESSION['ajaxKey'] = md5(uniqid(rand (), TRUE));
?>
<script src="clienthint .js"></script>
<script>
var ajaxKey = '<?PHP echo $_SESSION['ajaxKey']; ?>';
</script>
</head>
<body onLoad="showres ult(ajaxKey);">
<p>result: <span id="txtHint"></span>
</body>
</html>

I have a ajax jS file, the relavant part is

function showresult(str)
{

if (str.length==0)
{
document.getEle mentById("txtHi nt").innerHTML= ""
return;
}

xmlHttp=GetXmlH ttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}

var url = "read.php";
url=url+"?q="+s tr;

xmlHttp.onready statechange=sta teChanged;
xmlHttp.open("G ET",url,true) ;
xmlHttp.send(nu ll);
}

I have the read.php,

<?php

$q=$_GET["q"];

if ($q == $_SESSION['ajaxKey'])
{
$filename = "myfile.txt ";

$fp = fopen($filename , "rb") or die("Couldn't operate properly");

$buffer = fread($fp, filesize($filen ame));

echo $buffer;

}

else
{

echo "terminated".$_ SESSION['ajaxKey'];
}

?>
[/php]
The idea is:
1: index.php generate a session key
2: index.php call read.php
3: read.php check if session exists, if yes, then read it, if not, then it is a forging attempt

Basically, I plan to use session key to defeat forging. But in read.php, the session key is always empty.

Can anyone help me please?
Mar 4 '08 #1
3 3720
ronverdonk
4,258 Recognized Expert Specialist
Before we are able to help you, it would be nice to be able to read some orderly listed code. So enclose any code within the appropriate code tags. See the Posting Guidelines on how to do that.

moderator
Mar 4 '08 #2
eddierosenthal
11 New Member
do you have session_start() in each file ?
also why not use session_id for the key?
also why not use POST instead of get?
hth
Mar 4 '08 #3
ajaxphp
3 New Member
Thanks.

I have added the session_start, and have changed to POST. and it worked.

The problem is that I returned a Javascript function, and used eval(); but it seems that the script scope is wrong, I cannot use the function.
Mar 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
20071
by: dougwig | last post by:
I'm trying to handle the scenario where a user's session times out and and their ajax request triggers a redirection by the webserver (302 error?). I'm using Prototype 1.4 and the my works great with Firefox,but with IE6 the onFailure never gets called and the request never completes. My code: var ajaxReq = new Ajax.Request( url, {method: 'post', parameters:
2
3838
by: dmagliola | last post by:
Hello all, I'm experiencing a problem with ASP.Net for which I can't find a reasonable explanation, or any information. I'm currently developing an application that, through AJAX, asks the server for updated information to show, and can also send information to the server. To do this, I have an XMLHttpRequest that queries an Asynchronous HTTP
15
5948
by: Evil Otto | last post by:
My page loads, and calls an init() function that returns content to a div on the page, as well as setting a $_SESSION variable. The content it returns includes a link that calls the same variable, but I get an error that says the index isn't defined. The second two calls are AJAX-generated. The second call immediately echos the $_SESSION variable back after it sets it, and it sets it properly. But the subsequent request doesn't see...
3
2902
by: Beshoo | last post by:
hey gaiz plz I want to cerate log in system in ajax i do it but i have one problem AFTER I start thes session then press refresh key the session gone !!! in other words , after the user type hid username and password I send this information by XMLrequest to php file which verify the user name and the password if trure open the sesstion and get it name 'user' its become like this $_SESSION=$user;
13
3998
by: Marvin Zhang | last post by:
Hi, I'm not familiar with web programming, but I have a problem here. I have a page. When a user click one button on it, I will use AJAX to request a PHP script which will do a bunch of tasks, asynchronously. These tasks might take long time so I want to keep the user informed of the progress. The problem is that only the PHP script knows the progress, how can the web page gets these information from PHP script?
6
5156
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick application working. However, when attempting to implement the solution, the AJAX calls weren't updating the screen like the examples were and seemed not to fire until after the long running process had completed. I found the only real...
16
2038
by: deostroll | last post by:
Suppose I have an asp page that has a response.write(something) in a loop that would run for a considerable amount of time. Now, from my client browser can I trap those server response messages (using ajax) and print them on the browser? (The thing is the asp page is still running over at the server...while it is running I would want to monitor some variables that is part of the program that is running over at the server) --deostroll
0
7186
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along the way. First, deep linking is not something that a completely AJAX web site should be able to do by it's very nature of everything being on one page basically. So how can a person deep link to something that is on one page? This question...
1
4849
by: mbruyns | last post by:
i have been trying (and sometimes succeeding) to use the modalpopupextender to show various panels of controls on my asp pages. the strange problem that i keep on running into is that sometimes it works, displaying an awesome popup, while other times, it doesn't show a popup at all, but rather just page posts back and the panel becomes visible at the bottom of the page (where it is situated in the designer). what is even stranger to me is that...
0
9173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8911
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6533
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.