Journal #6357

Posted 14 years ago2010-01-29 00:53:53 UTC
As some might have already seen in previous journal posts, I'm working on a website for work.

As I have complete freedom over the design, I chose to make it in PHP to learn a new technology. It's going pretty well (special thanks to Penguinboy for pushing me in the right direction with CSS), but I'm facing a new obstacle right now. I don't think it's directly related to PHP though.

I need to make a conditional redirect, but I find I don't even know how to make a "normal" redirect.

The intended design is:

a.php has a textbox and a submit button. Enter the right string and you are redirected to b.php?event=stringentered . With a wrong string you just come back to the same prompt, maybe with an error message.

b.php only needs to send you back to a.php when no string is passed to it.

Right now, I only have one php page that does both, and it works, but I think it's a mess and I wonder if it would be "cleaner" with two pages instead.

Does anyone have an idea? :)

[PS. I could really use some text formatting features here such as bold, italics, etc. like in forum posts...]

17 Comments

Commented 14 years ago2010-01-29 01:45:22 UTC Comment #61813
I once did a website with a similar conditional redirect page, but instead of PHP, I used Javascript. Now, seeing as you are only talking about PHP, then I will point you to a PHP related resource.

It might or might not help you out on what you need, but at least it's free to use. Here is this little PHP redirect script that I found called "GoTo".

PHP GoTo will redirect a user to an URL that they select from a form. The allowed protocols are HTTP, HTTPS, and FTP. If the protocol is not specified with the form data, it defaults to HTTP.

You can download the script directly from here: http://www.lampscripts.net/node/3/release
Commented 14 years ago2010-01-29 01:58:18 UTC Comment #61805
Bad bad bad bad bad.
Use an MVC framework or DIE.

Also, Tito, please stop trying to respond to things you know nothing about.
Commented 14 years ago2010-01-29 02:42:15 UTC Comment #61814
Really Penguinboy? Let's put things in perspective here...........

I have already gotten paid over $22,000 from the past 3 years part time doing a few commercial websites for small business. I know it's not a lot, but it's a start. On top of that, my PayPal account gets about $400 to $600 a week from my various web stores that I created and own. The amounts will just be getting bigger as time goes on. And in April, I will be joining a web developer team as freelance consultant to work on an online state government project dealing with law enforcement. This job has a completion budget set at 4 million dollars! For this consultant job, I will be getting paid $36 an hour for the space of 4 months. I might not have the fancy I.T. degrees or be completely fluent with all the web developing technologies out there, but I do know enough for what I really need to know to get the job done. And you know what is the best part about all this from me? I am my own boss, I do not work for no body else, period. So, you still want to tell me that I don't know anything? There's a little saying that I always liked a lot: "Money talks, bullshit walks".
Commented 14 years ago2010-01-29 03:36:39 UTC Comment #61811
header("Location: a.php");
exit();

Redirecting isn't beautiful though, you might want to re-think your desgin.

You don't have to use MVC. MVC is very popular and for good reason, but there are other ways to code. You'll do just as well without it if you're making a really small website.
Commented 14 years ago2010-01-29 05:31:21 UTC Comment #61806
Uhh, no. I don't particularly care if someone chooses to hire you because you're good at flinging fancy buzzwords around. There are many people who are doing web development professionally and getting paid very good money for it, but don't actually have a clue about proper methodology or programming practices. You are one of these people. So while I'm happy for you to be churning out terrible websites for large sums of money, don't try to push your bad practices on learning developers until you get some proper knowledge on the subject.
Commented 14 years ago2010-01-29 15:06:30 UTC Comment #61815
Please. As we all know, there are plenty of experts on every field, and many more dumbasses that don't know shit about what they do, yet they earn large sums for doing it [wrong]. I wouldn't know what kind are you, and I don't want to venture saying something I shouldn't, but simply telling us what you are earning or what are your project budgets doesn't tell us anything about the quality of your work either.

Going back to the original topic, I'm not sure if potatis' solution would work, as what I need is a conditional redirect, and as far as I know, header statements cannot have any other statements (including if) before them.

And regarding Penguinboy's solution, I don't mind using a new technology but for now I'd prefer a simpler solution (especially because I only need this conditional redirect right now, maybe I'll use that in future projects)

So how do you think I should use die()?
Commented 14 years ago2010-01-29 16:29:44 UTC Comment #61812
"header statements cannot have any other statements (including if) before them."
That's a common myth. You can for example write
if(rand(0, 1)) {
header("Location: a.php");
exit();
}

You can't however, make changes to the header after you have output data (for example using echo or imagepng), because then it'll already have been sent to the client.

"So how do you think I should use die()?"
I don't think he's talking about the language construct (which by the way is equivalent to exit)...
Commented 14 years ago2010-01-29 18:41:46 UTC Comment #61816
Ok, I shall give that a try.

Also I'm starting to think he didn't say die(); but (grabbing sword) DIE!!!!!!!!!!
Commented 14 years ago2010-01-29 20:21:40 UTC Comment #61807
If your site is dynamic (i.e. if you're using a database) you should be using an MVC framework (or some other type of OO framework, whatever works for you). If you go with procedural PHP on a dynamic site, you'll regret it later. If you're just using a small PHP script on a static site (for an email form or something), then going procedural is okay. Make the decision depending on the type of website that you are creating.
Commented 14 years ago2010-01-29 21:10:07 UTC Comment #61817
It's not much of a dynamic site, as a matter of fact, the only part that uses PHP is a contact form and a clients page. Neither uses any database components. The clients page is the one referred to in the original post here; you enter a code the company gives to you, and it displays images from a folder with the name of the code you entered.
Commented 14 years ago2010-01-29 23:44:43 UTC Comment #61808
Yeah, a small procedural script is fine for stuff like that. As soon as you start working with databases, though, you definitely need to look into MVC. Don't make the same mistake I made with TWHL3 :P
Commented 14 years ago2010-01-30 00:09:10 UTC Comment #61818
OH MY! THERE'S NO DATABASE BEHIND THIS?! runs out the door flailing arms

There is a plan to include a new module in the future, to track employees schedules, but the database part of it was the first thing I thought of.

I can offer you to help with the database design for TWHL4 if you need a hand. It seems to be something I enjoy.
Commented 14 years ago2010-01-30 00:14:20 UTC Comment #61809
No, there IS a database behind this, but I didn't use a framework for it, is what I'm saying. I learnt PHP by making the site so I wasn't aware of frameworks at the time. I'm doing TWHL4 with the MVC framework I linked earlier but the current database design is fairly decent so I'm reusing it.
Commented 14 years ago2010-01-30 01:25:36 UTC Comment #61819
I can think of a few examples that use databases but no MVC - such as the PHPBB forum pack. It works well but I have never looked at the back end.

Do you know where could I learn more about a MVC framework? I've never used one.
Commented 14 years ago2010-01-30 02:33:13 UTC Comment #61810
PHPBB uses it's own custom framework, so it probably isn't based on MVC.

If you have access to a copy of visual studio 2008 you should grab ASP.NET MVC and use some of their resources - even if you don't have a ASP.NET hosting service (just use it locally to learn MVC), ASP MVC is by far the best and easiest MVC framework to use, and it's a great one to start on because there are so many resources for it. C# is easy as cake, you can pick it up quite quickly. Also recommended is the book on ASP MVC by Steve Sanderson (easy to find in ebook form) to help you learn the basics.

If you don't wanna go that way you may have a bit more trouble figuring it out but there are lots of resources on the topic floating around the internet, but ASP MVC is by far the best way to get a concept of MVC. Once you get the basics down, you can move on and choose a framework for PHP. My favourite is Kohana (linked above), but you could also look at CodeIgniter, CakePHP, and others. The Wikipedia article might help, as well as various tutorials and documentation on the websites of the MVC frameworks themselves.

If you want to you can grab me on IRC and I can give you some pointers to start you off on MVC.
Commented 14 years ago2010-01-30 16:45:43 UTC Comment #61820
Thanks, I might do that too :)
Commented 14 years ago2010-02-03 17:54:10 UTC Comment #61821
If anyone cares, I did what potatis said.

if (isrequestvalid())
{
header("location: a.php");
exit();
}

It's just what I needed.

You must log in to post a comment. You can login or register a new account.