Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7783533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:52:01+00:00 2026-06-01T19:52:01+00:00

I would like to set php session with ajax. there is my function to

  • 0

I would like to set php session with ajax.
there is my function to set session in session.php.

public static function set($key, $value) {  
    $_SESSION[$key] = $value;
}

and I have confirm javascript alert and if I click on yes, i want to set session.
How can I set session simply?

EDIT 16.4.2012 15:31 GMT:+1

David: thanks for an answer, it looks clearly. I tried it, but doesn´t do anything at my pc. So I have: a.php

<script src="http://code.jquery.com/jquery-latest.js"></script>
<p>I would like to say: </p>
<script>
    $("p").append("<strong>Hello</strong>"); //check jquery is working 
    $.get('a.php', function(data) {
        // do something on success?
    });
</script>

b.php (in same folder)

<script type="text/javascript">
    alert('aaa');
</script>
<?php
echo "print by b.php";
?>

and I run a.php in browser

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:52:03+00:00Added an answer on June 1, 2026 at 7:52 pm

    You can have a simple PHP page which does nothing more than set the value (using the code you already have), and then have jQuery call that page:

    $.get('setsessionvalue.php', function(data) {
      // do something on success?
    });
    

    If you want to expand it, you can make the call more general by passing values on the query string:

    $.get('setsessionvalue.php?key=someKey&value=someValue', function(data) {
      // do something on success?
    });
    

    Then you’d pull those query string values from the $_GET collection on the server just like any other page.

    From the server-side code’s perspective, it’s not different than any other page. You just don’t need to render a UI (and can render data if you want, probably easiest in JSON format). The JavaScript code is just calling that page like any other page.

    It’s worth noting, of course, that a user can call this page manually if they want to. So don’t use this as any kind of “security” for session values. If a user goes to the URL:

    setsessionvalue.php?key=someKey&value=someValue
    

    then they would be able to set their session values manually. Even overriding the values you’ve already set (which they can see in the JavaScript code). I wouldn’t be particularly comfortable exposing the structure of session values (especially their keys) to client-side code like that. It seems like a short distance away from having some security holes in your site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to set values on an url like this: <a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note'&user='$username'> Then
ExpressionEngine version is 1.6.8 and NOT upgradeable. I would like to set a session
I used the same command syntax above as in Mysql and would like set
I would like to set some limits/boundaries on the amount of pixels we can
I would like to set up an XSD such that it enforces something like
I would like to set up an error page which will diplay a message
I would like to set the new child documents to default to a set
i would like to set the date format of all the dates in my
I would like to set up a proxy object in the application NIB file.
I would like to set up a project to be published as HTML using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.