/    Sign up×
Community /Pin to ProfileBookmark

Clear sessions and redirect

Hi

I’m trying to log out a user and then redirect them to the home page but the process of clearing the session seems to prevent the redirect. The code is below. Does anyone have a proper way of doing this as I’m sure I’m missing something really obvious.

[code=php]
<?
ob_start();

session_start();
session_unset();
session_destory();

function redirect($location) {
header(‘Location: ./’ . $location);
}

redirect(‘index.php’);

?>
<html>
<head>
<script type=”text/javascript”>
function redir() {
window.location = ‘index.php’;
}
</script>
</head>
<body onload=”redir();”>
<!– this bit in case the php fails! –>
<a href=”index.php”>Home</a>
</body>
</html>

[/code]

Thanks.

PS, I’ve noticed after I posted this that I misspelt destroy – Oops.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 18.2009 — [code=php]<?
ob_start();

session_start();
session_unset();
session_destory();

function redirect($location) {
header('Location: ./' . $location);

////////////////////////
exit;
///////////////////////
}


redirect('index.php');[/code]
Copy linkTweet thisAlerts:
@NogDogFeb 18.2009 — To cover all contingencies:
[code=php]
<?php
session_start();
setcookie(session_name(), '', time()-42000, '/');
session_unset(); // only needed if register_globals is enabled
$_SESSION = array();
session_destroy(); // check your spelling
session_write_close(); // just in case
header("Location: http://www.example.com/index.php"); // should be a full URL
exit;
?>
[/code]
Copy linkTweet thisAlerts:
@zingmatterauthorFeb 18.2009 — That looks better. thanks
×

Success!

Help @zingmatter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...