/    Sign up×
Community /Pin to ProfileBookmark

PHP Redirect?

How can I redirect to another page in an If statement? Heres what the pseudo-code looks like…

[code=php]
if(condition){
do this
}else{
redirect to another page
}
[/code]

Can this be done?

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@JickMar 21.2007 — If I understand you correctly, I believe you're looking for something like this:[code=php]if(condition) {
do this
} else {
header('Location: mypage.php');
exit();
}[/code]
It uses the [B][FONT="Courier New"]header[/FONT][/B] function. Please let me know if I misunderstood the question.
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — Does that work if I have something like ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>[/QUOTE]


above it or not?
Copy linkTweet thisAlerts:
@m_sahalyMar 21.2007 — hi r_sole

yes dear , what Jick said works if you have something like what u stated above . and here is another method as well >

you can use this HTML code :

print '<meta http-equiv="refresh" content="2;url=mypage.php">';

it may not work properly unless u use ( ob_start(); ) at the very begining of your script .
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — When the page is redirected will my variable's values be passed over to the new page. Like in this ...

[code=php]
if(something){
do something
}else{
$test = true;
header('Location: page.php');
exit();
[/code]


Would $test be true in page.php?
Copy linkTweet thisAlerts:
@m_sahalyMar 21.2007 — yes ofcourse , but this is in case you appended them to the URL and you can appened as many variables as you like in the URL like the following :

header('Location: page.php?var1=value1&var2=value2');
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — Thanks m_sahaly.
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — Just a question... Why is that forms can easily pass values on to another page but without a form you need to use the querystring.
Copy linkTweet thisAlerts:
@m_sahalyMar 21.2007 — you surly know that evern forms may append there content to the Querystring if its method is GET .the alternatives are as you said Query string , session ,DB ,and files may be .

I donot know if i answered ur Q or not .but feel free to ask .
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — What I meant was, forms don't use the querystring but still manage to pass on values to other pages. Isn't there some way for me to pass on a certain value without the querystring?
Copy linkTweet thisAlerts:
@m_sahalyMar 21.2007 — sessions
Copy linkTweet thisAlerts:
@itbeingsMar 21.2007 — [code=php]if(!empty($_SESSION['User']) && $_SESSION['User']['Log.Member'] == "Member.Logged"){
#Allow user to see the member area
}
else{
##
#Your Login page already defined in 'config.inc' if any
define('LogIn', 'http://www.domain.com/LogIn/');
#
##
header("Location: ".LogIn."?_rPath=".$_SERVER['HTTP_REFERER']."?".$_SERVER['QUERY_STRING'].""); //Go to login page and remember the last page view
}


///Now on you login page
//If user is authenticated
##
#Your Member page already defined in 'config.inc' if any
define('MemberArea', 'http://www.domain.com/Members/');
#
##
if(!empty($_SERVER['QUERY_STRING']) || !empty($_GET['_rPath']))
$ReturnPath = $_GET['_rPath'];
else:
$ReturnPath = MemberArea;
endif;
#Forward user to the Return Path
header("Location: ".$ReturnPath."");
[/code]
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — Sweet, sessions work great.
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — Is it possible to have a link that, when clicked, will change a session value?
Copy linkTweet thisAlerts:
@itbeingsMar 21.2007 — It's possible. All you have to do is to set a value to it as soon as it get to the other page.
[code=php]
//On page A
$_SESSION['A'] = "Me";
echo '<a href="look.php">Check it</a>';


//On look.php
$_SESSION['A'] = "What I like now";
[/code]


Does that help. I mean is that what you are asking?
Copy linkTweet thisAlerts:
@r_soleauthorMar 21.2007 — Yeah thats what I was asking. THanks dude.
×

Success!

Help @r_sole 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...