Harlequins needed a late try as they came from behind to beat Gloucester and keep alive their hopes of reaching the Heineken Cup quarter-finals.
After an early penalty apiece the hosts went ahead through Matt Hopper's try.
A converted James Simpson-Daniel try helped Gloucester into an 11-10 half-time lead, but Quins went back in front through a Nick Evans penalty.
Freddie Burns restored Gloucester's lead but Mike Brown's converted try ensured Quins stay second in Pool Six.
They will go into the final round of games on 16 points, two points behind leaders Toulouse and well placed to either top the pool or secure one of the two best runners-up spots.
Premiership leaders Quins now travel to bottom side Connacht in their final game on Friday, while Toulouse face a difficult trip to face Gloucester at Kingsholm.
It was a desperately nervy win and three missed penalties from the usually reliable Evans, plus captain Chris Robshaw's headstrong decision to turn down a kickable penalty and go for a scrum with 10 minutes to go, nearly cost the Londoners dear.
But, in the end, their refusal to accept defeat got them over the line and they now look well placed to reach the last eight.
Evans started by pulling an early penalty chance and, when young English prospect Burns was handed a similar chance, the Gloucester fly-half made no mistake.
Evans hit the target with his next attempt before featuring in a fine move that led to the opening try.
Sweet hands from the Quins fly-half and try scorer Hopper helped create space for Ugo Monye to attack down the left and, when the ball was recycled, Hopper showed superb footwork to cross untouched by the posts.
Evans converted and, with Burns missing a penalty from the restart, Quins led 10-3 after a quarter of the match and looked set to secure an easy victory.
But Burns then landed his second successful kick as Gloucester enjoyed a period of superiority which culminated in their first try.
Simpson-Daniel came on an arcing run from deep to make a classic outside break, which took him through Hopper's attempted tackle and over, although Burns could only hit the post with the conversion.
Quins were gaining the upper hand in the scrum but they failed to take advantage as Evans missed a kickable penalty either side of the break.
The former New Zealand number 10 did edge them back in front on 47 minutes at 13-11 but the visitors hit back when replacement prop Rupert Harden turned the tide at scrum time, winning a penalty which Burns banged over from distance.
It was not a great night for either fly-half with the boot and Burns missed the chance to extend Gloucester's lead to four points.
Harlequins' hopes of staying in Europe were hanging by a thread but a Danny Care break earned them a five-metre scrum, only for Robshaw's bizarre decision to go for another scrum rather than take the easy points when they were awarded a penalty.
Gloucester then had loose-head prop Nick Wood yellow-carded for collapsing but, at the fifth attempt, Quins lost control and Care was pulled back for offside after dotting down.
It looked as though Gloucester had been handed the match but, with seven minutes to go, Brown touched down Care's kick ahead after a wicked bounce wrong-footed the covering Charlie Sharples.
Evans converted and, although Gloucester mounted a series of late attacks, Harlequins, roared on by a passionate Stoop crowd, kept their line and their Heineken Cup hopes intact.
VIEW FROM THE DRESSING ROOM
Harlequins director of rugby Conor O'Shea:
"This time last year we would probably have lost that game. We are enjoying learning what it is like every week to play at this level, with a heck of a lot on the line every time you go out there.
"We have a saying on the board 'live by the sword, die by the sword'. It hasn't cost us and we can learn from winning rather than learn from losing. Teams only get used to that playing matches when you have everything to lose.
"We go into the last round of the Heineken Cup with an opportunity to go through. I know what Connacht are like down there. We will have to be at our best to win. We are in with a chance."
Gloucester coach Bryan Redpath told BBC Radio Gloucestershire:
"The boys' effort was outstanding. That's the benchmark we should set whether we're playing home or away. I can't fault the players.
"We can't let our standards slip. Potentially there's only 12 matches remaining this season and we've got to be in every single one with that mindset."
"We've got to finish this Heineken Cup campaign with our heads up and look for a win against Toulouse at the weekend."
Harlequins: Brown; Williams, Hopper, Turner-Hall, Monye; Evans, Care; Lambert, Gray, Johnston, Kohn, Matthews, Fa'asavalu, Robshaw, Easter.
Replacements: Marler for Lambert (51), Brooker for Gray (51), Vallejos for Matthews (57).
Not Used: Fairbrother, Wallace, Bolt, Clegg, Casson.
Gloucester: May; Sharples, Tindall, Fuimaono-Sapolu, Simpson-Daniel; Burns, R Lawson; Wood, S Lawson, Chistolini, Hamilton, Brown, Strokosch, Qera, Narraway.
Replacements: Harden for Chistolini (50), Dawidiuk for S Lawson (68), James for Brown (69), Murphy for Qera (69), Lewis for R Lawson (72), Cox for Narraway (77).
Not Used: Mills, Trinder.
Sin Bin: Wood (69).
Ref: Pascal Gauzere (France).