Jump to content

unusual header() problem.


d3cl4n

Recommended Posts

Hello,

 

I have recently switched to using php shorthand if statements to save space and make my code easier to read. However, for some reason which is boggling me this does not work.

 

if ($login_button){

($status_check->status == "Banned"	? $error = header("Location: banned.php?user=".$user_name."")	:NULL);
($status_check->status == "Dead"	? $error = header("Location: dead.php?user=".$user_name."")	: NULL);
// there are a few more of these statements to do with username/pass checks

if (!$error){
// do lots of cool sql stuff.
}else{
echo "".$error."";
}

 

 

Now, this does not work. But if I change the:

$error = header("Location: banned.php?user=".$user_name."")

to:

$error = 'Some output here'

Then it would work and echo the error.

 

All help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/115728-unusual-header-problem/
Share on other sites

Thanks for the suggestion but it doesn't work.

In "shorthand php" you don't put the semicolon after defining variables ect.

 

the format is like this:

 

($something == "Something" ? $if_true = 'text' : $if_false = 'text');

 

No semicolons atall.

Any other suggestions?

 

Ternary operators often make code harder to read.  And if you truly wanted easier to read and/or easier keep track of code, you'd be writing your applications in an OOP mindset rather than procedural nonsense.  If you honestly think you're saving any time with that "shorthand if statement", you're pretty mistaken.

Thanks for your input everyone, but please if your not trying to help me with this problem just don't reply. The way I code makes it easier for me.

 

thorpe:

I've tried just doing ? header()

but when that didn't work I put it in the variable.

 

Do you know a way to make it work?

I solved the problem myself by switching to meta refresh.

 

$meta_banned =	"<meta http-equiv=\"refresh\" content=\"0;url=banned.php?user=$user_name\"/>";
$meta_dead =	"<meta http-equiv=\"refresh\" content=\"0;url=dead.php?user=$user_name\"/>";

if ($login_button){

($status_check->status == "Banned"	? $error = $meta_banned  : NULL);
($status_check->status == "Dead"	 ? $error = $meta_dead	   : NULL);

 

 

But thanks for all of your excellent criticism ;D.

But seriously, to the people who did try to help thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.