PHP Session Variables and AJAX

This one is a beauty… 

I have recently been writing a web application that combined AJAX and PHP Session Variables.  I soon encountered a problem where it appeared that the session variables were not available form the code loaded by the AJAX call.

The application basically fires off an AJAX call to load a PHP page when a link is selected, and then populates a <DIV> on the same page with the parsed results of that PHP page.  Session variables were being used to store some basic ifnromation about the logged in user – user name, etc. – and I was hoping to make use of these in the AJAX loaded code.

After some serious head scratching it became clear that the PHP session variables, whilst being available everywhere else in the application, were not available from any code loaded with AJAX.

Now, whether this is a known issue or not, or whether I’m being dim, I’m not entirely sure and would welcome any input people have to offer.  However, in the meantime I’ve ended up passing over a couple of extra parameters with the AJAX GET statement and have used those to regenerate the user related data within the AJAX loaded code. 

Because the application is OO designed, this wasn’t a major problem, but actually recognising the problem was a bit time consuming; hopefully this post will save someone else the head-scratching!

Life is what happens…

when you’re making other plans, according to John Lennon!

And it certainly seems to be the case that blogging is not what happens when you’re assailed by the requirements of life and the ‘day job’!

What’s been difficult in the last couple of weeks is stocking ideas up for the blog but never seeming to be in a position when I can actually settle down and do anything.  Now, there are two ways I can look at this; one is that I’ve been so busy that I’ve just not been able to touch base with a computer, or the other is that I’ve just prioritised blogging downwards for a short while!

I’m afraid that I have to plead guilty to the latter; a side effect (which I should have guessed!) of Facebook was that I suddenly found myself touching base with friends I’d lost contact with a decade ago, talking about new projects and new ideas.  All good stuff but something of a phenomenal show stopper!

So…be prepared for Facebook to seriously damage your ability to do anything else!

Of course, the day job also had it’s impact, along with a little more socialising with real life friends than usual. 

But, enough excuses.  I have ideas, I have a little more time, so I’d better get going.

ASP.NET Gridview and CSS

Here’s a quick tutorial on combining a style sheet with an ASP.NET gridview control.  The only reason I’ve picked on the Gridview control is that I’m using it extensively at the moment.  This is something that threw me a little when I first had to use the Gridview with a CSS file.  Whether I was being particularly dense or not I’m not sure, but for what it’s worth, here’s a quick ‘getting it working’ tutorial.

This is far from complete – let’s just call it a traditional web development mindset being applied to ASP.NET.  I always find that making the initial breakthrough’s the hard part – I hope this short article eases the way for you!

The tools…

I’m assuming you’re using Visual Web Developer for the purposes of this tutorial.  If you’re doing it ‘the hard way’ with just a programmer’s editor then I’ll detail that separately.  In each case ‘Website’ is the name of the folder containing the files that make up the website.

Within Visual Web Developer

On the default.aspx page, go in to Design Mode and drop a Gridview object and a data source object to suit your data source (this will depend upon what you’re using for your data for the view, and isn’t relevant to the tutorial – the CSS side of things will apply to all data sources).

Now, right click on the Website folder in the Solution Explorer window.  From the displayed menu, select ‘Add ASP.NET Folder, and from the newly displayed sub-menu select ‘Theme’.  A folder called ‘App_Themes will be created, and within it a new folder called ‘Theme1’ will be visible, available for you to type in the name of the display theme that you’re going to create.  (For the sake of this tutorial, an ASP.NET theme is a collection of visual effects that can be applied to a website and the controls therein – including style sheets)  With startling originality, we can call it ‘Test’.

Now, right click on the newly created ‘test’ folder within the ‘App_Themes’ folder, and select ‘Add New Item’.  A dialogue will be displayed that will allow you to select, amongst other things, a Style Sheet.  Select this, and rename the style sheet added to the Test Theme to ‘Test’.

We now have an empty style sheet – open it up and enter the following:

.gridtitle {color: fuchsia;}

This adds a CSS class to the Style Sheet called ‘gridtitle’.  All it does is sets the colour of the text to a rather disgusting (but definitely noticable) fuschia pink colour.Obviously, in the real world we would now proceed to put in to our style sheet anything we pleased.  But for the purposes of this tutorial, this will do.Now, back to our Default.aspx page.  Select the ‘Source’ view.  Modify the first line of the file to read as follows:

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”Default.aspx.vb” Inherits=”_Default” StylesheetTheme=”Test” %>

All we’ve done is added the StyleSheetTheme attribute with a value of ‘Test’.  This basically attaches the Style Sheet from our Test theme to the page.Now, we have a Style Sheet, containing a class.  We now need to tell a particular part of the Gridview control to inherit the visual style set by this class.

For the sake of demonstration, find the <Headerstyle> tag within the asp:Gridview structure, and modify the tag to read as follows : <HeaderStyle CssClass=”gridtitle” />

This assigns the gridtitle class from our Style Sheet to the Header style.If you run the page, you will see the Header Row of the Gridview rendered in bright pink. And that’s how to do it!

Using a Text Editor

If you’re constructing the site using a text editor, then the syntax used is as above.  the main thing you have to do manually is to create the necessary folder structure under the ‘Website’ folder to support the Themes.  In the same folder as the Default.aspx file, create a folder called ‘App_Themes’.  Now, within that folder, create a further folder called ‘Test’.  Within the ‘Test’ folder create a stylesheet file called ‘test.css’ and define the gridtitle class within it.

Now return to the Default.aspx file and make the changes to it outlined above.

Again, that’s all there is to it!

The Bus Book – w/c 31st March – Libra

Don DeLillo’s Libra takes on the great rift in the American psyche; that which opened up on 22nd November 1963 when Kennedy was assasinated.  Whatever the nature of the man, one thing became true form that point on – that bad things can indeed happen to and in the Home of the Free.

This is a labyrinthine book on a number of levels.  It is a semi-fictional account of the life and death of Lee Harvey Oswald.  The author makes no attempt to separate fact from fiction; indeed, as Oswald has entered the status of myth it would probably be nigh on impossible to do so.  The book follows Oswald’s youth, his time in the US Marines, his assignment to the U2 base at Atsuga, his defection, his return, and the fateful day in November 1963 when he and history collided on Dealey plaza.

The book also follows the activities of three disaffected CIA veterans of the Bay of Pigs invasion, and how their initial thoughts of a faked assasination attempt on JFK eventually give way to soemthing more sinister.

Parts of the story are seen through the eyes of Nicholas Branch, a retired CIA agent who is now tasked with assembling the definitive story of the assasination of JFK.  He is provided with all sorts of information and meta-information about the assasination – even down to the dreams of witnesses to the event.  A CIA information specialist he knows as ‘The Curator’ keeps the information coming, thickening the fog and building new paths in to an already infinitely complex maze.

Over the years it is quite possible that no other single event in human history with one exception has been examined so closely with so little agreement as to what actually happened.  And the other event is the Crucifixion.

The rest of the book is almost stream of conciousness from Oswald, the CIA agents, Jack Ruby and all the other bit part players in the drama.  The labyrinths I mentioned are of information, conspiracy, identity and intention.

 It’s a stunning book.  I am actually quite haunted by parts of it.  As a Sunday Times critic wrote – “This, you feel, is America, and the bad news starts here.” CS Lewis, who wrote about the ‘Inner Ring’ of people more in the know than you are, also died on 22nd November 1963; I’ve often regarded this as ironic. 

Read it – it reminds me, for some strange reason, of ‘The Name of the Rose’.  I have no idea why.

A final observation…I bought this book in Leeds and started reading it on the evening train back to Sheffield.  A young lady opposite asked me what I was reading, and I told her.  A young man in a neighbouring seat then looked up and showed what he was reading; The Illuminatus! Trilogy – a novel about an all-encompassing conspiracy.  Whether The Curator would forward notes on such a random meeting to Nicholas Branch…who knows.

The guilty pleasure that is H P Lovecraft

Over the last couple of days I’ve taken a break from Don DeLillo’s ‘Libra’ and have returned to one of my all time favourite horror / science fiction writers, H P Lovecraft.  In particular, I’m re-reading his novella ‘The Case of Charles Dexter Ward’ – an everyday tale of international grave robbing, ghoulish possession and dealings with dark forces, spread over the centuries.

HPL is a definitely un-PC writer to admit to enjoying.  Even as a fan there are some phrases used that today slap you in the face as being patronising or racist, but given the attitudes prevalent in much fiction in the first 25 years of the 20th Century, I’m willing to cut some slack.  His ‘purple prose’ is well known – just mention the words eldritch, un-nameable and squamous as adjectives to anyone with a passing knowldge of his books, and you’ll immediately elicit his name.

I recently bought replacement volums of his stories – my existing ones had fallen apart after 20 years of reading.  The first time I read any Lovecraft at all was in a Corgi edition in my teens – I remember the book as having a purple cover – very apt, I thought – and it was part of a ‘Science Fiction Classics’ series.  The first stories of his I remember are not, oddly enough, from the C’thulhu Mythos – they were ‘The Colour out of Space’ and ‘The Shadow out of Time’ – pretty much straight science fiction in most respect.  His Venus set ‘In the Walls of Eryx’ was pure 1930s science fiction, with the ilmage of Venus as an overgrown jungle world.  Once I got hooked in to the C’thulhu mythos, it was downhill all the way.  I also have a neat collection of ‘Mythos Stories’ from other authors which are great fun, and wrote my own Necronomicon related short story set in Victorian England.  Great fun!

As for the Mythos, it’s struck me recently that many of the Mythos stories I actually like best are not by Lovecraft himself!  I think one of the amazing things about the Mythos structure is how it’s been used (and occasionally abused) by an incredibly wide range of writers; just as most writers will do a Holmes Homage, most science fiction authors will end up doing a Mythos related tale somewhere along the way.  My favourite Mythos tales probably include those of Stephen King (‘Crouch End’) and Colin Wilson – particularly his ‘Return of the Lloigor.

I remember some years ago killing time in London one evening (during my film making days) in a Cybercafe on Tottenham Court Road writing a Mythos tale based around a creature that could inhabit electronic networks.  Oddly enough, the email in which I sent the story to myself mysteriously disappeared in transit.  It did make me wonder…

Back to Charles Dexter Ward – true Gothic Horror.  There is a section of the book where even now I have to sit back and think hard, following the disguises and double identities of the characters in the novel – but it’s a great story.

If you’ve never read any Lovecraft, and you don’t mind a bit of prose that is slightly purple, I suggest you start with ‘The Call of Cthulhu’ to kick you off on the Mythos stories.  ‘The Music of Erich Zann’ is an interestingly understated short story that always reminds me of H G Wells’ ‘The Platner Story’.  ‘The Mountains of Madness’ is a good read as well – set in the Antarctic and deals with the discovery of ancient alien life on Earth (part of the Mythos).

In the words of an old newspaper review…go forth to HP Lovecraft and shudder!

The Bus Book – w/c 24th March – The Social Entrepreneur: Making Communities Work

Andrew Mawson (now a Cross Bencher in the House of Lords) has written a very important book with this work.  It chronicles his experiences in Bromley-by-Bow, an impoverished area of East London, from his arrival there as a United Reform Church Vicar in 1984 through his driving of the the development of social enterprises, health centres and other vital services to this community – with the whole hearted support of the community and despite the best efforts of local and national Government.

I picked this book up almost by accident and I’m so glad that I did.  I found it incredibly motivating – whilst there are some good, practical hints in there it’s more of a history and how problems were coped with as they arose – I think the author should settle down and write a further book ‘Practical Lessons from Bromley-by-Bow’ – I would certainly buy a copy!

Rather than pontificate too much, I’ll refer you to an article by Andrew Mawson himself:

I have to say that the experiences he documents reflect the sort of experiences I’ve had in a much smaller way with many funding organisations and local authority, national Government and EU bodies over the years.  This was the original impetus for my CommunityNet and CommunityHost projects, and reading this book has made me want bigger and better things for the projects that I’m involved with.

I agree with Mawson’s basic tenets; it is essential that anyone wishing to develop their community needs to get buy in from the bottom up – from the people who are suppsoed to benefit from the efforts being expended.  This means involving these people, and letting them become the architects of their own solutions – not watchers and observers.  This is the ‘CommunityNet Philosophy’ that I elaborate on here; I now feel in excellent company.

An excellent and motivational book; one that I whole heartedly recommend!