University of Oregon

PHP redirects if the headers have already been sent.

I ran in to a situation with some inherited code today. The developer wanted to redirect users to a course listing page if they landed on a date/time listing that couldn’t be found. See this code below; they check for an empty $dateTime var and create a log message before attempting to redirect the user.
if (empty($dateTime)) {
  $logger->err('DateTime with the id of '.$_REQUEST['dt_id'].' cannot be located.');
  Header( 'Location: course_list.php'); // Go back to the course list
Unfortunately the headers had already been sent to the browser and the Header function call was ignored and the user only saw a partially loaded web page with no body content. In the past I had implemented a fall-back with a meta and js redirect but I thought that could be improved upon. Stack Overflow had some nice responses to a previous question and one of them included Redirect function that checked if the headers were already sent: headers_sent(). I liked that. So I added my fall back to that function. Next I swapped that Header function call with the Redirect function call:
if (empty($dateTime)) {
  $logger->err('DateTime with the id of '.$_REQUEST['dt_id'].' cannot be located.');
  Redirect('course_list.php'); // Go back to the course list
Problem solved. Here’s the function (note: it’s wrapped in a function_exists() call to make sure I don’t declare this twice at any point):
  function Redirect($url, $code = 302) //from
    #echo 'redirecting';
    if (strncmp('cli', PHP_SAPI, 3) !== 0)
      #echo 'cli';
      if (headers_sent() !== true)
        if (strlen(session_id()) > 0) // if using sessions
            session_regenerate_id(true); // avoids session fixation attacks
            session_write_close(); // avoids having sessions lock other requests
        if (strncmp('cgi', PHP_SAPI, 3) === 0)
            header(sprintf('Status: %03u', $code), true, $code);
      header('Location: ' . $url, true, (preg_match('~^30[1237]$~', $code) > 0) ? $code : 302);
      } else { //headers have already been sent. Use Meta refresh with js location.href fallback
        <em>This page has moved.</em><br />
        If you are not redirected automatically please follow this link: <a href=\"<?php echo $url; ?>"><?php echo $url; ?></a>.
        <meta http-equiv="Refresh" content="1;
        URL=<?php echo $url; ?>">
        <script language=javascript>
        setTimeout("location.href='<?php echo $url; ?>'",1);
      }//end if headers_sent


Protected: Selling a home…

This content is password protected. To view it please enter your password below:

Updating select lists in bulk – w/ jquery, drupal

So I wrote this script 1 year and 1/2 ago about bulk label operations (Drupal 7 CCK – Creating ‘Label Bulk Operations’). Well the updateLabels() function from that code came in handy today. I needed to update dozens of CCK formats from various defaults to editable. If you’ve used editable fields you know it can come in handy but if you have a lot of fields you’ll be in a carpel tunnel conundrum if you try to change then all. So I pulled up the updateLabels() function opened the Firebug console and set all the various defaults to editable. And then back again. (more…)

Concatenate fields in a Fillable form

Recently someone referenced an old blog post: use data from one field to populate other fields in an acrobat fill-in forms and asked me how to concatenate multiple field values into one.

So I put together a sample pdf.


Drush enable all inactive modules

I’ve been using make files a lot lately and when ever I’m ready to release an updated file I like to verify that all the project versions are up to date. To leverage Drupal’s update functionality each project needs to be enabled. So I typically generate a new site from the make file and enable all the projects with drush. The key to that is the combination of commands:
drush en `drush pm-list --status="disabled,not installed" --pipe`
Here I’m enabling a list of module; specifically drush’s list of disabled and not installed modules.
drush pm-list --status="disabled,not installed"
That code gets you part of the way there. The addition of this:
makes it a script-able list. Then when I surround that with back-ticks and feed it to
drush en `...`
I’m in business. (more…)

Drupal 6 – Contact Override custom module

Just created a quick and dirty custom module: Contact Override. I posted it on bitbucket:   Nothing too exciting, just an override to the D6 contact form. It appends the senders email into the body of the email.   We had noticed that while emails from the Drupal contact form contained the senders email in the reply field that email address was often lost when forwarding the message. Here’s the readme: (more…)

Reponsive design and the iPad viewport

I had been noticing that the iPad’s viewport in webkit browsers was odd but I couldn’t put my finger on it. So I opened Safari on the iPad and ran some JS in firebug lite; the result: 768×504 in landscape and 768×928 in portrait mode. Not what I expected at all. The code I ran in firebug lite was:
alert(jQuery(window).width() + 'x ' + jQuery(window).height());
(prepend javascript: for use in the URL bar) Bottom line: the viewport width is 768px in both portrait and landscape mode. That’s a little odd. (more…)

Working on a file in two git branches at one time

I have a file in two different branches of a project that is so drastically different (hundreds upon hundreds of changes) that merging was becoming unreasonable. I’ll call that file CONVOLUTED.module.
I wanted to go old-school and compare the files side by side. Cutting and pasting changes with regular commits so I could test the incremental changes as I manually merged the files.
I figured there are several options to do this.

git branch – auto setup rebase = always

I was revisiting Randy Fay’s Rebase Workflow post and went down the ‘rebase’ rabbit hole again. Each time I learn a little more. Probably because I understand a little more… This time I landed here: ‘Simpler Rebasing …’ and as a result I’ve updated my git config to rebase on pull: git config –global branch.autosetuprebase always Note: this use of rebase isn’t the history altering version, rather an adjustment to the order of operations when pulling from a remote repo. The goal being to lay your local changes on top of the remote code each time you pull. Here’s how it looks in my git config now:
  autosetuprebase = always #Ref: #alternatively use: git config --global branch.autosetuprebase always
We’ll see how it goes. I’ll add this to my public git_bash_files repository if I like it.

Clean up your curl – fixing relative links in scraped content

A few years ago I Added jQuery browser check for ODT and Skillport eLearning. This week I updated that functionality and noticed a relative link in some remote content I was scraping via cURL. The problem being that the relative link needed to be fully qualified. Here’s the old table that I cURL from the Skillport support site: Screen Shot of the old compatibility table Here’s the updated table: a view of the table pulled in from skillport The culprit is the “Click here to view Skillsoft’s Browser Support Statement” at the top of the table. “here” links to /44007.htm which isn’t helpful. (more…)