All posts by Ruturaj Vartak

How sessions work in PHP

HTTP is a stateless protocol. Which means that every request the browser makes to the server cant be identified by the server as a subsequent request of that user/IP/browser or a brand new request.

HTTP doesn’t understand who is requesting. So how do sessions manage to make HTTP look intelligent? The Answer lies in the request-response model with data.


When a normal request is made, eg my website, the minimalistic data passed by the client/browser is this

GET / HTTP/1.1
Host: ruturaj.net

The server responds by giving the output. But when a developer does a session_start();, What actually happens is, the PHP engine sets a PHPSESSID cookie. This data is sent from the Server as Set-Cookie header. So the response goes somewhat like this

HTTP/1.x 200 OK
Date: xxxx
Set-Cookie: PHPSESSID=<32charhexvalue>; expires=xxxx
...

Now considering the browser does accept the cookies, it saves the PHPSESSID cookie. Consequently the server also creates a file in the specified directory (by default on Linux as /tmp) as /tmp/sess_32charid.

Now when another request is made by the user/browser, the Cookie header is passed through the GET request back to the server, something like this…

GET /session2.php HTTP/1.1
Host: ruturaj.net
Cookie: PHPSESSID=<32charid>; othercookies=othervalues;

The session2.php, for example, is setting a value of name in session, by this

$_SESSION['name'] = $name_obtained_from_somewhere;

Now as the script finishes, the script flushes all the $_SESSION data into the /tmp/sess_32charid file associated to that session id. It saves all the data in the serialized format

Consider the browser makes another request to session3.php where $_SESSION['name'] is echoed. Now when the request is made, just like previous case, the PHPSESSID is passed in the cookie.

Now as mandated by php.net, that every page where sessions should be needed, a session_start(); is required. So as soon this function is invoked, PHP checks if the browser’s request had any PHPSESSID cookie sent in the header, as it was sent in our case, PHP Engine will open /tmp/sess_32charid file (with the same session id) and unserialize the contents of the file. It then assigns the values of the unserialized data structures to the $_SESSION variable.

The simple echo $_SESSION['name']; will now be able to output the name!! Sessions working…

On a session_destroy();, PHP sends a destructive, previous timestamp cookie for PHPSESSID and unlinks or deletes the /tmp/sess_32charid file. This ensures that no reference of that session is left.

References

  • http://in3.php.net/manual/en/session.configuration.php

Siddhagad Trek

Monsoons had just arrived and we couldn’t resist the first one of the wet season, Siddhagad. Located near Murbad, between Gorakhgad and Bhimashankar, it was a good outing for us, Vj, Mallik, R.A. Jo B.A, Sneha, Swati and Kudrat. We could’ve hiked the final Balekilla, but as we were on a tight one-day schedule, we had to reach up to the cave atop the Siddhagad machi and return back.

Siddhagad Photos
Siddhagad Photos

Travelling Directions

  • Get a train for Kalyan Railway Station (Central Railway)
  • A ST bus to Murbad
  • Another ST bus to Narivali
  • A walk starts which converts to a trek soon – towards Siddhagad

Approximate time of Trek: 6-7 hrs (including the return to the base village)

Hai junoon – New York | Guitar Chords

This entry is part 12 of 41 in the series Guitar

This new song, is not very different in strumming or chords, runs in the scale of D, with Dsus4 to add higher note and moves in D, A and G.

Intro

D ... D

sus4

A ... A

sus4

Song

Na .. Na nana na na na
D             D

sus4

  A
Nana na na na na
              G
Nanana na na na
             A

Yaaron ji bhar ke ji le pal
D                D

sus4

  A
lagta hai aaj kal
              G
daur apna aayegaa
              A

yaaron khud pe ho yakeen
D              D

sus4

  A
toh zindagi haseen
              G
tujhe kal bulayegaa
                A

Strumming

Down ... Down Down Up Up Down Up Down

The strumming pattern is a little cagey, modifications appreciated.

Fedora 11 on Acer 4736Z

I finally managed to get a new Laptop, Acer 4736Z. I’d to wait before Fedora 11’s download was complete and I could install it.

Overview

  • Graphics card works out of the box (1366×768 resolution)
  • Sound is detected well
  • Wireless and bluetooth are working
  • The Web Camera is detected and working well
  • The Brightness control doesn’t work
  • The gsynaptics had to be installed to enable tap on the touchpad

All and all it looks good. Here is a screenshot.
Fedora 11 on Acer 4736Z

Pulseaudio CPU usage
Great work, Pulseaudio in F11 uses lesser CPU as compared to Fedora 10 Pulse Audio

Fedora 10 Pulse Audio High CPU Usage

Guys using Fedora 10, must’ve seen this problem of Pulseaudio taking high CPU usage. After a lot of tweaking, I came to this. From System -> Preferences -> Hardware -> Sound

Change all the drop downs to ALSA. Just make sure u “Test” and hear the tone before applying the changes.

pulseaudio with lower cpu usage

Update

I’ve done this yum remove pulseaudio. This removes the root cause 🙂

Live – Selling the drama guitar tabs

This entry is part 22 of 41 in the series Guitar

I heard this song recently by Live and found it interesting if it could be played on a Guitar. Here is what I’ve managed.

Tuning: E standard, Capo on 1st fret

// Initial Tab played on 6th or the base E string
E |-6b--6-4-2--|
When u bend on the 6th fret, bend hard a full half note.
And return and play 6th fret again with 4 and 2 in quick succession.

And to love: a god
C              F
And to fear: a flame
C              F
And to burn a crowd that a name
Dm             F           C

And to right or wrong
C               F
And to meek or strong
C              F
It is known, just screem it from the wall
Dm                  F                C

Ive willed, Ive walked, Ive read
C               F           C
Ive talked, I know, I know,
    F       C       C
Ive been here before
C             G

 

MySQL DB Pie Graph

Its same as what http://blog.olindata.com/2009/02/using-the-google-graph-api-with-mysql-stored-functions/ had posted, I’ve just updated a little. And there are no smart quotes here. So u can copy paste 😉

DELIMITER $$

DROP FUNCTION IF EXISTS `test`.`FNC_GOOGRAPH_DB_SIZE`$$
CREATE FUNCTION `test`.`FNC_GOOGRAPH_DB_SIZE` (
  p_chart_type CHAR,
  p_height INT,
  p_width INT) RETURNS varchar(3000) CHARSET latin1
  READS SQL DATA
BEGIN
  /* Author:    Walter Heck - OlinData */
  /* Date:      20090216 */
  /* Note:      After an idea by Alex Gorbachev - Pythian */
  /*            http://www.pythian.com/blogs/1490/google-charts-for-dba-tablespaces-allocation */

  /* variable declaration */
  DECLARE v_done BOOLEAN default false;
  DECLARE v_url varchar(3000);

  DECLARE v_schema_name varchar(3000);
  DECLARE v_data_length_sum int;
  DECLARE v_data_length_total int;
    
  DECLARE v_legend_labels varchar(3000);
  DECLARE v_chart_labels varchar(3000);
  DECLARE v_chart_data varchar(3000);

  /* Cursor declaration */
  DECLARE c_schema_sizes cursor for
      select
        t.table_schema,
        round(sum(t.data_length + t.index_length) / 1024 / 1024) as data_length_schema
      from
        information_schema.tables t
      group by
        t.table_schema
      order by
        t.table_schema;

  /* Handler declaration */
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = true;

  /* Initialize the variables */
  SET v_legend_labels = '';
  SET v_chart_labels = '';
  SET v_chart_data = '';

  /* Get the total data length + index_length for all tables */
  select
      round(sum(t.data_length + t.index_length) / 1024 / 1024) as data_length_total
  into
      v_data_length_total
  from
      information_schema.tables t;
    
  /* Open the cursor */
  OPEN c_schema_sizes;

  /* Loop through the cursor */
  get_data: LOOP

      /* Fetch the next row of data into our variables */
      FETCH c_schema_sizes INTO v_schema_name, v_data_length_sum;

      /* if there is no more data, v_done will be true */
      IF v_done THEN
        /* Exit the loop */
        LEAVE get_data;
      END IF;

      /* Add the schema name to the labels for the legend */
      IF v_legend_labels = '' THEN
        SET v_legend_labels = v_schema_name;
      ELSE
        SET v_legend_labels = concat(v_legend_labels, '|', v_schema_name);
      END IF;

      /* Add the total size of the schema to the labels */
      IF v_chart_labels = '' THEN
        SET v_chart_labels = v_data_length_sum;
      ELSE
        SET v_chart_labels = concat(v_chart_labels, '|', v_data_length_sum);
      END IF;

      /* Get the percentage of the total size as the graph's data */
      IF v_chart_data = '' THEN
        SET v_chart_data = ROUND(v_data_length_sum / v_data_length_total, 2) * 100;
      ELSE
        SET v_chart_data = concat(v_chart_data, ',', ROUND(v_data_length_sum / v_data_length_total, 2) * 100);
      END IF;

  END LOOP get_data;

  /* Close the cursor */
  CLOSE c_schema_sizes;
    
  /* Build up the google graph url */
  SET v_url = 'http://chart.apis.google.com/chart?';
  SET v_url = CONCAT(v_url, 'cht=', p_chart_type);
  SET v_url = CONCAT(v_url, '&chs=', p_width , 'x', p_height);
  SET v_url = CONCAT(v_url, '&chtt=Database Sizes (MB)');
  SET v_url = CONCAT(v_url, '&chl=', v_chart_labels);
  SET v_url = CONCAT(v_url, '&chd=t:', v_chart_data);
  SET v_url = CONCAT(v_url, '&chdl=', v_legend_labels);

  /* return the url as the function's result */
  RETURN v_url;
END$$

DELIMITER ;