Jump to content

preventing PHP page from outputting text (part of Ajax app)


cbassett03

Recommended Posts

Here is a segment of code that I'm using in a PHP script, which is called as part of an AJAX HTTP Request:

 

<?php

   
    $result = $user_conn->execQuery($sql_query);
    
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $resultText .= '<option value="'.$row['id'].'">'.$row['display_text'].'</option><br />'."\n";
    }

    echo $resultText;
?>

 

NOTE: I created a MySQL class with custom functions, so execQuery() is a custom function I created that actually runs the query() command for SQL.

 

Anyway, as you can see, to get the results back to the JavaSCript calling request function, I used ECHO.  Is there an alternative so that if someone where to run just this PHP script directly, it wouldn't output any text (because right now it will)?

 

(I asked this a few weeks ago but seem to have "lost" the post.)

Link to comment
Share on other sites

You can check for $_SERVER['HTTP_X_REQUESTED_WITH'] and optionally see if it == xmlhttprequest.

 

Also, you might want to generate a token in PHP, stick it in the session, echo it to JS so it can use it as a var, send this token with each AJAX request and check for it in the PHP script.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.