Marcus2000 Posted February 16, 2011 Share Posted February 16, 2011 Hi guys, Please can anyone help me with a simple bit of PHP. I'm just starting out, so its all a bit confussing to me. Basically I need a bit of code that does something like this: My end user types in a password on a form on the html page. The pasword is checked by the php page. If pasword is correct the user gets sent to another page within my site. If pasword is incorrect the user gets sent to an error page within my site. I know this is real simple stuff, but as i said i'm realy new to this, hope some one can help. Cheers People. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/ Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 Here is some code that you can use to check for a 'hard coded' password, however you'd almost never use this in a real life situation, instead you would connect to a database and check the password against a stored one. $rightPass = lucky; //if a form has been submitted - otherwords, the user has submitted the password if(isset ($_POST("submit"))){ if($_POST("password") == $rightPass){ //user has entered the correct password. //either give them a hyperlink to the next page, or do this: header("next_page.php"): }else{ //user has entered and incorrect password echo "You have entered and incorrect password"; } Hope that helps Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174825 Share on other sites More sharing options...
lalnfl Posted February 16, 2011 Share Posted February 16, 2011 Make sure the header is like this: header("Location: http://www.example.com/example.php"); exit(); Make sure you exit out of the rest of the script. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174826 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 Make sure the header is like this: header("Location: http://www.example.com/example.php"); exit(); Make sure you exit out of the rest of the script. Thanks. I was going straight from memory and I'm a bit of a novice myself, so couldn't remember exactly how it should have gone. Do you have to specify the full http link?? Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174827 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 Wow, that was a quick response, thank you. I'm still having a little trouble though, i'm getting a syntax error on the header lines. this is your code slightly modified for what i want. <?php $rightPass = ZF2011; if(isset ($_POST("submit"))){ if($_POST("c") == $rightPass){ header("success_page.php")} ****error on this line**** else{ header("error_page.php")} ****error on this line**** ?> Please can you let me know what i'm doing wrong? Cheers, Marcus. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174833 Share on other sites More sharing options...
lalnfl Posted February 16, 2011 Share Posted February 16, 2011 Make sure the header is like this: header("Location: http://www.example.com/example.php"); exit(); Make sure you exit out of the rest of the script. That is how they show it over at php.net. Just to be safe I would do it that way. Thanks. I was going straight from memory and I'm a bit of a novice myself, so couldn't remember exactly how it should have gone. Do you have to specify the full http link?? Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174837 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 as lalnfl has corrected me, use the format header('Location: http://www.example.com/'); Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174838 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 Thanks for helping guys but ive still got a problem, i get this message when i test the code: Fatal error: Can't use function return value in write context in D:\Sites\mywebsite.com\form.php on line 16 This is the code i'm using: <?php $rightPass = ZF2011; if(isset ($_POST("submit"))){ if($_POST("c") == $rightPass){ header('Location: http://www.mywebsite.com/success.php'); exit(); } else{ header('Location: http://www.mywebsite.com/error_page.php'); exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174852 Share on other sites More sharing options...
lalnfl Posted February 16, 2011 Share Posted February 16, 2011 Do you have the right address in the header? Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174853 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 Try with double quotes instead of single.. Not sure if it would make much difference, but worth a try. Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174854 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 I have the right address in the headers, i just changed it before posting the code here. Tried double quotes and still the same error. Any ideas? Cheers people. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174862 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 Can I please see your form html code? Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174867 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 Sure, no problem this is the code for the form. <form id="form1" name="form1" method="post" action="form.php"> <p> <label for="password"></label> <input name="password" type="text" id="c" maxlength="12" /> </p> <p> <input type="submit" name="download" id="c" value="Download" /> </p> </form> Hope this helps you guys out. This is driving me mad Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174870 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 You need to change the if isset to have download in the double quotes, or c. I'm not sure exactly which one, but having submit in there does nothing. The reason I used submit is because it's commonly used as the name for the submit button, you've used download. Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174871 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 Cheers Denno, I changed the code as you advised but I still get this errror: Fatal error: Can't use function return value in write context in D:\Sites\mywebsite.com\form.php on line 16 this is the HTML: <form id="form1" name="form1" method="post" action="form.php"> <p> <label for="password"></label> <input name="password" type="text" id="c" maxlength="12" /> </p> <p> <input type="submit" name="download" id="c" value="Download" /> </p> </form> And this is the PHP: <?php $rightPass = ZF2011; if(isset ($_POST("download"))){ if($_POST("c") == $rightPass){ header("Location: http://www.mywebsite.com/success.php"); exit(); } else{ header("Location: http://www.mywebsite.com/error_page.php"); exit(); } } ?> Cheers for sticking with us Denno mate, I thought you had it on your last post. God this is anoying. Hope we can find an answer. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174876 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 Alright, I think I've fixed it for you.. I'm a douche bag. Such a simple error... if(isset ($_POST["download"])) Note the square brackets instead of the curly.. Try that. Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174881 Share on other sites More sharing options...
denno020 Posted February 16, 2011 Share Posted February 16, 2011 Should also add that for each time you check $_POST, you need to use square brackets, not the rounded ones.. Sorry man Denno Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174883 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 Fixed!!! ..... well kind of. This was stressing me out big time, so i deleted the code i wrote and made an exact coppy of a simular thing that my work mate has used. Its not exactly what i wanted but i will make do with it. Thanks for all your help people, one day i hope php will actually make sense to me. Cheers Guys, Marcus. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174888 Share on other sites More sharing options...
Marcus2000 Posted February 16, 2011 Author Share Posted February 16, 2011 Cheers Denno for all your help, your a star. I'm sure i will be back will more php headaches in the future for you to solve! Cheers Dude. Quote Link to comment https://forums.phpfreaks.com/topic/227827-if-and-else-statements-complete-newbe-please-help/#findComment-1174890 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.