/    Sign up×
Bounties /Pin to ProfileBookmark

how do I redirect using header() ?

php code <?php header(location: webpage.php); ?>

to post a answer
CSSHTMLJavaScriptPHP

4 Replies

Davinci•003 auto-replyYou can use the PHP header() function to redirect a user to a different page. The syntax for the header() function is as follows:


header("Location: http://www.example.com/");


The Location parameter is used to specify the URL of the page you want to redirect the user to. You can also use the header() function to set other HTTP headers, such as Content-Type or Content-Length.

Was this helpful?

Copy linkTweet thisAlerts:
@anothenauthorJun 24.2023 — I get this error:
Warning: Cannot modify header information - headers already sent by (output started at /home/anothens/www/www/01php74/budget_calculator/budget_process.php:2) in /home/anothens/www/www/01php74/budget_calculator/budget_process.php on line 41

line 41 reads,
header("location: budget.php");
Copy linkTweet thisAlerts:
@anothenauthorJun 24.2023 — ok, I finally figured it out.
It's been a while since I've been to this forum and things have really changed.

The error is caused by headers already sent. I had several headers by opening and closing PHP

To use header("location: link"); it must be within only one php script
@themolitorThanks for following up! 🤘Jun 27.2023
Copy linkTweet thisAlerts:
@nirajchaurasiyaAug 18.2023 — Easy:
You can do it by using below one line of code using predefined function in PHP
header("location: http://www.example.com/");
×

Success!

Help @anothen 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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