Lamez Posted July 1, 2008 Share Posted July 1, 2008 I am fairly new to php, but I was wondering is there some sorta goto command that will let me goto a certain part of the script under a certain label? I am working on a password gate, but the script is really dynamic so I was wondering is there a goto command? example: echo "correct"; goto form :form echo "register"; -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/ Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 Every C, Java, etc teacher I had have told me that goto is built into most languages, but you should never never never ever ever ever use goto. And that it has been proven that anything you can do with goto, you can do with if else statements. And they've said you can get yourself very confused and in a lotta trouble using thrm. I suggest taking the time to learn loops and if else statements. You'll love them after a little time learning them. Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578930 Share on other sites More sharing options...
Lamez Posted July 1, 2008 Author Share Posted July 1, 2008 well I know about IF and ELSE statements and Loops statements, but I am in a jiffy with this script here, and I cannot find anyway around it. Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578932 Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 The only form of a goto command I've seen in PHP is continue. It can accept optional numeric arguments, however this is only used in loops to skip the current iteration. Example: <?php while (list($key, $value) = each($arr)) { if (!($key % 2)) { // skip odd members continue; } do_something_odd($value); } $i = 0; while ($i++ < 5) { echo "Outer<br />\n"; while (1) { echo " Middle<br />\n"; while (1) { echo " Inner<br />\n"; continue 3; } echo "This never gets output.<br />\n"; } echo "Neither does this.<br />\n"; } ?> Reference: http://uk2.php.net/manual/en/control-structures.continue.php Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578935 Share on other sites More sharing options...
Lamez Posted July 1, 2008 Author Share Posted July 1, 2008 well maybe you guys can help me out with this, <?php $path = ""; $title = "Register"; $login = "no"; $ban = "no"; include ($path."main/include/cons/head.php"); echo '<p class="header">Register</p>'; $r = mysql_query("SELECT * FROM `pgs`"); $row = mysql_fetch_array($r); if ($row['reg'] == (0)){ echo '<p class="maintext">Register is disabled at the moment.</p>'; }else{ $r = mysql_query("SELECT * FROM `site_status`"); $row = mysql_fetch_array($r); if($row['pass_on_reg'] == ('1')){ ?> <form action="" method="post" name="pw_gate"> <label> </label> <table width="336" border="0"> <tr> <td colspan="3">Please enter the password to continue</td> </tr> <tr> <td width="79">Password</td> <td width="156"><input name="password" type="password" id="password" maxlength="30"></td> <td width="87"><label> <input type="submit" name="register" id="register" value="Continue"> </label></td> </tr> </table> <label></label> </form> <?php if(isset($_POST['register'])){ if ($_POST['password'] == ($row['reg_pass'])){ echo "Correct Password"; //show register form some how? }else{ echo "Incorrect Password"; unset($_POST['register']); } } }else{ ?> <SCRIPT TYPE="text/javascript"> function numbersonly(myfield, e, dec) { var key; var keychar; if (window.event) key = window.event.keyCode; else if (e) key = e.which; else return true; keychar = String.fromCharCode(key); // control keys if ((key==null) || (key==0) || (key== || (key==9) || (key==13) || (key==27) ) return true; // numbers else if ((("0123456789").indexOf(keychar) > -1)) return true; // decimal point jump else if (dec && (keychar == ".")) { myfield.form.elements[dec].focus(); return false; } else return false; } //--> </SCRIPT> <?php if($session->isAdmin()){ echo '<p class="maintext">Please use the <a href="'.$path.'_admin/admin.php">Admin Center</a></p>'; }else if($session->logged_in){ echo '<p class="maintext">Please Wait!</p>'; }else if(isset($_SESSION['regsuccess'])){ if($_SESSION['regsuccess']){ echo '<p class="maintext">You are now registered, you may <a href="'.$path.'>index.php">Login</a>.</p>'; } else{ echo '<p class="maintext">Regisration Error.<br /><a href="register.php">Try Again</a>?</p>'; } unset($_SESSION['regsuccess']); unset($_SESSION['reguname']); }else{ ?> <p class="maintext"> <font color="#FF0000">All fields are required.</font> <form action="<?php echo $path; ?>main/include/process.php" method="POST"> <table width="100%" height="186" border="0"> <tr> <td width="149" height="24">Username</td> <td width="154"><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>" /></td> <td width="1066"><? echo $form->error("user"); ?></td> </tr> <tr> <td height="24">Password</td> <td><input name="pass" type="password" id="pass" value="<? echo $form->value("pass"); ?>" maxlength="30" /></td> <td rowspan="2"><? echo $form->error("pass"); ?></td> </tr> <tr> <td height="24">ReType Password</td> <td><input name="pass2" type="password" id="pass2" value="<? echo $form->value("pass"); ?>" maxlength="30" /></td> </tr> <tr> <td height="24">E-Mail</td> <td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>" /></td> <td><? echo $form->error("email"); ?></td> </tr> <tr> <td height="24">First Name</td> <td><input type="text" name="first" maxlength="50" value="<? echo $form->value("first"); ?>" /></td> <td><? echo $form->error("first"); ?></td> </tr> <tr> <td height="24">Last Name</td> <td><input type="text" name="last" maxlength="50" value="<? echo $form->value("last"); ?>" /></td> <td><? echo $form->error("last"); ?></td> </tr> <tr> <td height="24" colspan="3"><font color="#666666">This will not be shared with anyone, this will only be use if we need to contact you. Ex: 8775551234 (with area code)</font></td> </tr> <tr> <td height="24">Phone Number</td> <td><input name="phone" type="text" id="phone" onkeypress="return numbersonly(this, event)" value="<? echo $form->value("phone"); ?>" maxlength="10" /></td> <td><? echo $form->error("phone"); ?></td> </tr> <tr> <td> </td> <td><input type="hidden" name="subjoin" value="1" /> <input name="submit" type="submit" value="Signup!" /></td> <td align="right"><a href="<?php echo $path; ?>support/forgotpass.php">Forgot Password?</a></td> </tr> </table> </form> </p> <?php } } } include ($path."main/include/cons/foot.php"); ?> as you can see where is shows the echoed out correct password, some how I need to get to the registration form, but how? Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578939 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 What are you trying to do? I'm positive there's a cleaner and easier way of getting it done. Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578943 Share on other sites More sharing options...
GingerRobot Posted July 1, 2008 Share Posted July 1, 2008 There are lots of different ways you can do it. For example, you could wrap your registration form in a function and call that, or you could set a variable and check it's value prior to the showing the form. I'm going to direct you to a tutorial on form handling. It really is very good, and i suggest you read it: http://www.rbredlau.com/drupal/node/10 Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578946 Share on other sites More sharing options...
Lamez Posted July 1, 2008 Author Share Posted July 1, 2008 well dannyb785 when the user enters the correct password, I want it to goto the registration form, but I boggled on how to get this done, I have so many if and else statements in the script, I do not know what to do. GingerRobot: I am reading the page you replied with... Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578952 Share on other sites More sharing options...
GingerRobot Posted July 1, 2008 Share Posted July 1, 2008 I have so many if and else statements in the script, I do not know what to do. And therein lies your problem: lack of planning means you've over-complicated the script. Try to take more time to plan what you're trying to do and plan your logic. Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578959 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 I have so many if and else statements in the script, I do not know what to do. That's because you're trying to output an entirely different page depending on what variables are what value. I don't understand... they put the right password and they are redirected to the registration page? Isn't that before registering?? Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578963 Share on other sites More sharing options...
Lamez Posted July 1, 2008 Author Share Posted July 1, 2008 I think I might have a solution, I will get back to you guys Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-578974 Share on other sites More sharing options...
Lamez Posted July 1, 2008 Author Share Posted July 1, 2008 I figured it out, I used headers redirects, and sessions here is my final code: register.php <?php ob_start(); $path = ""; $title = "Register"; $login = "no"; $ban = "no"; include ($path."main/include/cons/head.php"); echo '<p class="header">Register</p>'; $r = mysql_query("SELECT * FROM `pgs`"); $row = mysql_fetch_array($r); if ($row['reg'] == (0)){ echo '<p class="maintext">Register is disabled at the moment.</p>'; }else $r = mysql_query("SELECT * FROM `site_status`"); $row = mysql_fetch_array($r); if($row['pass_on_reg'] == ('1')){ if (!isset($_SESSION['pass'])){ //if($_SESSION['pass'] !== (1)){ header('Location: '.$path.'main/include/passgate.php'); //} }else ?> <SCRIPT TYPE="text/javascript"> function numbersonly(myfield, e, dec) { var key; var keychar; if (window.event) key = window.event.keyCode; else if (e) key = e.which; else return true; keychar = String.fromCharCode(key); // control keys if ((key==null) || (key==0) || (key== || (key==9) || (key==13) || (key==27) ) return true; // numbers else if ((("0123456789").indexOf(keychar) > -1)) return true; // decimal point jump else if (dec && (keychar == ".")) { myfield.form.elements[dec].focus(); return false; } else return false; } //--> </SCRIPT> <?php if($session->isAdmin()){ echo '<p class="maintext">Please use the <a href="'.$path.'_admin/admin.php">Admin Center</a></p>'; }else if($session->logged_in){ echo '<p class="maintext">Please Wait!</p>'; }else if(isset($_SESSION['regsuccess'])){ if($_SESSION['regsuccess']){ echo '<p class="maintext">You are now registered, you may <a href="'.$path.'>index.php">Login</a>.</p>'; } else{ echo '<p class="maintext">Regisration Error.<br /><a href="register.php">Try Again</a>?</p>'; } unset($_SESSION['regsuccess']); unset($_SESSION['reguname']); }else{ ?> <p class="maintext"> <font color="#FF0000">All fields are required.</font> <form action="<?php echo $path; ?>main/include/process.php" method="POST"> <table width="100%" height="186" border="0"> <tr> <td width="149" height="24">Username</td> <td width="154"><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>" /></td> <td width="1066"><? echo $form->error("user"); ?></td> </tr> <tr> <td height="24">Password</td> <td><input name="pass" type="password" id="pass" value="<? echo $form->value("pass"); ?>" maxlength="30" /></td> <td rowspan="2"><? echo $form->error("pass"); ?></td> </tr> <tr> <td height="24">ReType Password</td> <td><input name="pass2" type="password" id="pass2" value="<? echo $form->value("pass"); ?>" maxlength="30" /></td> </tr> <tr> <td height="24">E-Mail</td> <td><input type="text" name="email" maxlength="50" value="<? echo $form->value("email"); ?>" /></td> <td><? echo $form->error("email"); ?></td> </tr> <tr> <td height="24">First Name</td> <td><input type="text" name="first" maxlength="50" value="<? echo $form->value("first"); ?>" /></td> <td><? echo $form->error("first"); ?></td> </tr> <tr> <td height="24">Last Name</td> <td><input type="text" name="last" maxlength="50" value="<? echo $form->value("last"); ?>" /></td> <td><? echo $form->error("last"); ?></td> </tr> <tr> <td height="24" colspan="3"><font color="#666666">This will not be shared with anyone, this will only be use if we need to contact you. Ex: 8775551234 (with area code)</font></td> </tr> <tr> <td height="24">Phone Number</td> <td><input name="phone" type="text" id="phone" onkeypress="return numbersonly(this, event)" value="<? echo $form->value("phone"); ?>" maxlength="10" /></td> <td><? echo $form->error("phone"); ?></td> </tr> <tr> <td> </td> <td><input type="hidden" name="subjoin" value="1" /> <input name="submit" type="submit" value="Signup!" /></td> <td align="right"><a href="<?php echo $path; ?>support/forgotpass.php">Forgot Password?</a></td> </tr> </table> </form> </p> <?php } } include ($path."main/include/cons/foot.php"); ?> passgate.php <?php ob_start(); $path = "../../"; $title = "Register"; $login = "no"; $ban = "no"; include ($path."main/include/cons/head.php"); $r = mysql_query("SELECT * FROM `site_status`"); $row = mysql_fetch_array($r); if($row['pass_on_reg'] == ('1')){ echo '<p class="header">Register</p>'; ?> <p class="maintext"> <form action="" method="post" name="pw_gate"> <label> </label> <table width="336" border="0"> <tr> <td colspan="3">Please enter the password to continue</td> </tr> <tr> <td width="79">Password</td> <td width="156"><input name="password" type="password" id="password" maxlength="30"></td> <td width="87"><label> <input type="submit" name="register" id="register" value="Continue"> </label></td> </tr> </table> <label></label> </form> </p> <?php $submit = $_POST['register']; $pass = $row['reg_pass']; $npass = $_POST['password']; if (isset($submit)){ if ($pass === ($npass)){ session_start(); $_SESSION['pass'] = 1; // store session data header('Location: '.$path.'register.php'); }else{ echo "Incorrect Password"; } } }else{ header('Location: '.$path.'register.php'); } include ($path."main/include/cons/foot.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/112736-solved-a-goto-command/#findComment-579028 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.