php - Should I call exit() after calling Location: header?

后端 未结 5 1217
情书的邮戳
情书的邮戳 2020-11-22 03:38

After calling the redirect function header, should I call exit or not?



        
5条回答
  •  自闭症患者
    2020-11-22 04:06

    You should, just like @rgroli explains. If you do not want to bother with brackets, you can also call header() IN exit():

    if(sth) exit(header("Location: http://example.com"));
    

    Location header in HTTP/1.1 always requires absolute path see the note here.

    Note: This is not a hack, since the exit code is used only if the parameter is integer, while header() produces void (it exits with code=0, normal exit). Look at it as exit_header() function like it should be after Location header.

提交回复
热议问题