PHP - Regex Breaks Down When It Encounters A º Sign Or An &
Hi there. I have a javascript code that packages an array as a string and sends it to a php mailer, which then separates the string into an array for mailing using a preg_split regular expression that searches for a comma followed by an html tag.
$myWrongArray = preg_split("/,(?=<)/",$myVar); // lookahead splits the string when the comma is before a tag (the opening bracket < ) Everything works great EXCEPT when the string contains a degree sign or an &, in which case the array is returned up to the degree or & and then stops. I can’t for the life of me understand it. Any ideas? Similar TutorialsI'm currently working on a browser based mmorpg. First things first, here is an example of my battle.php <?php include_once 'connect.php'; session_start(); include_once 'logo.php'; ?> <link href="style.css" rel="stylesheet" type="text/css" /> <div id="login2" div align="center"> <?php if (isset($_SESSION['player'])) { $player=$_SESSION['player']; } else { echo "Not Logged in <br><br> <A href='login.php'>Login</a>"; exit; } ?> </div> <?php $playerinfo="SELECT * from players where name='$player'"; $playerinfo2=mysql_query($playerinfo) or die("could not get player stats!"); $playerinfo3=mysql_fetch_array($playerinfo2); include_once 'statpanel.php'; $pid = $playerinfo3['id']; $name = $playerinfo3['name']; $playerhp = $playerinfo3['hpoints']; $playerattack = $playerinfo3['attack']; $playerdefense = $playerinfo3['defense']; if ($playerhp < 1) { echo "You are dead!" ; echo "<br><a href='inn.php>Heal at the lake"; exit; } ?> <div id="table"> <?php if (isset($_GET['randid'])) { $randid=$_GET['randid']; $iteminfo="SELECT * from inventory where randid='$randid' AND id ='$pid'"; $iteminfo2=mysql_query($iteminfo) or die("could not get item stats!"); $iteminfo3=mysql_fetch_array($iteminfo2); if (!$iteminfo3['name']) { } else { $iname = $iteminfo3['name']; $stats = $iteminfo3['stats']; $statadd = $iteminfo3['statadd']; $type = $iteminfo3['type']; if ($type == "healing") { $newhp = $statadd + $playerhp; if ($newhp > $playerinfo3['maxhp']) { $newhp = $playerinfo3['maxhp']; } $updateplayer="update players set hpoints='$newhp' where id='$pid'"; mysql_query($updateplayer) or die("Could not update player"); $updateitem="DELETE from inventory where id='$pid' AND randid='$randid' limit 1"; mysql_query($updateitem) or die("Could not delete item"); $playerhp = $newhp; echo "Used " . $iname . " and recovered " . $statadd . ".<br>"; } }} $creature = $playerinfo3['creature']; if ($creature != 0) { $creatureinfo="SELECT * from creatures where id='$creature'"; $creatureinfo2=mysql_query($creatureinfo) or die("could not get the creature you were fighting!"); $creatureinfo3=mysql_fetch_array($creatureinfo2); } else { $creatureinfo="SELECT * from creatures order by rand() limit 1"; $creatureinfo2=mysql_query($creatureinfo) or die("could get a creature!"); $creatureinfo3=mysql_fetch_array($creatureinfo2); $cid = $creatureinfo3['id']; $updateplayer="update players set creature='$cid' where name='$name'"; mysql_query($updateplayer) or die("Could not update player"); } $creature = $creatureinfo3['name']; $creaturehp = $creatureinfo3['hpoints']; $creatureattack = $creatureinfo3['attack']; $creaturedefense = $creatureinfo3['defense']; ?> </div> <div id="player"> <?php /////player info echo "<u> " . $playerinfo3['name'] . "</u><br>"; echo "Hit points = " . $playerhp . "<br>"; echo "Attack = " . $playerattack . "<br>"; echo "Defense = " . $playerdefense . "<br><br><br>"; ?> </div> <div id="creature"> <img src="images/<?php echo $creatureinfo3['imgurl'];?>" <br> <?php ///////creature info echo "<u> " . $creatureinfo3['name'] . "</u><br>"; echo "Hit points = " . $creaturehp . "<br>"; echo "Attack = " . $creatureattack . "<br>"; echo "Defense = " . $creaturedefense . "<br><br><br>"; ?> </div> <div id="options"> <?php echo "<a href='attack.php'>Attack</a>"; echo "<br><a href='usemagic.php'>Use Magic</a>"; echo "<br><a href='useitem.php'>Use Item</a>"; echo "<br><a href='index.php'>Exit Arena</a>"; ?> </div> <div id="logout"> <?php echo "<br><a href='logout.php'><img src='images/logout.gif'>"; ?> </div> Particular functions I'm interested in for zone creation. -Pay to get in & have the initial icon switch to a now usable adventure zone. (done) -When 'adventuring' in this link. Battle php randomizes encounters between 'combat encounters' and 'non-combat' encounters. (can't get this right, and removed my utter failures from the code above as to not clutter it. Pulls randomly from the Creature DB just fine but when I try to add in the 2nd,non-combat,db..it pulls nothing.) -NonCombat encounters dish our reward. (currently my non combats do not give reward. Unlike a standard creature encounter where a players gold and exper get updated on a victory...I dont know how to immediately trigger the change on encounter & I'm epically unsure how to craft the $updateplayer= to be so broad as to include all possibilities of improvement but only to utilize the one being called upon in the encounter) -I'm also interested in updating my battle.php to include a dropdown list of usable spells/items instead of the current method of Attacking/going back/selecting magic or items/going back/...repeat. It works, sure, but its nasty..Any ideas on how to call on the database for user specific data to fill in a drop down menu? I keep getting errors that are associated with one-off and one-time occurrences. I want to tell the script to just restart itself from the beginning if it encounters any type of error, whether it by a fatal error, or any other kind. is there a way to do this? Hi, We currently have an RSA key in XML format, and am trying to sign it with PHP and phpseclib, however we are struggling to do it. We have managed to decrypt and encrypt it, but can not find a way to sign it. They key has been generated with .NET. Cheers Okay, here's what I'm doing. It's sort of like a market script. When someone wants to buy something, they choose from a drop down the item they wish to buy. However, there is a setup cost for it to be started. Then, based upon the length they choose (anywhere from 1 - 12 months), the cost will add up. My questions a Is it possible to assign a setup cost? How can I make it so that, while some things will be, say $2 a month, others will be $3 or other amounts? Here's what I have so far, to try and figure this out. Feel free to laugh... Or if you could point me to an example of something like this, I'd be very appreciative! Thanks!! function ChangeInitialCost(){ $cic = ($_POST['newpremium']); if (($cic == "clan") || ($cic == "guild") || ($cic == "house") || ($cic == "militia")){ } And the form I have: (I haven't entered a submit button yet...) Code: [Select] <table width="200" border="1"> <form action="premium.php" method="post" name="purchase"> <tr> <td>Premium:</td> <td><select id="newpremium" name="newpremium"> <option value="casino">Casino</option> <option value="clan">Clan</option> <option value="guild">Guild</option> <option value="house">House</option> <option value="militia">Militia</option> </select></td></form> </tr> <tr><form action="premium.php" method="post" name="length"> <td>Length:</td> <td><select id="length" name="length"> <option value="0">-----</option> <option value="1">One Month</option> <option value="2">Two Months</option> <option value="3">Three Months</option> <option value="4">Four Months</option> <option value="5">Five Months</option> <option value="6">Six Months</option> <option value="7">Seven Months</option> <option value="8">Eight Months</option> <option value="9">Nine Months</option> <option value="10">Ten Months</option> <option value="11">Eleven Months</option> <option value="12">Twelve Months</option> </select></td></form> </tr> <tr> <td>Cost:</td> <td>SOME TYPE OF FUNCTION</td> </table> Hello, The number sign does not seem to be showing up after a GET request. I've set up a script for an example: text.php: Code: [Select] <?php session_start(); mysql_connect("localhost", "*****", "*******************9") or die(mysql_error()); mysql_select_db("*****") or die(mysql_error()); function safe($var) { $var=mysql_real_escape_string(addslashes(htmlspecialchars($var, ENT_QUOTES, 'UTF-8'))); return $var; } $N=safe($_GET['N']); echo"$N"; ?> Say the url is text.php?N=### None of the symbols show up. Thankyou, GB. So this is how the data is returned for the API I'm using:
REC INFO[p!]=p EXP DATE[p43]=03-20-15 PCODE1[p44]=1 PCODE2[p45]=uSo I'm trying to filter on either of the keys like 'PCODE2[p45]' or 'PCODE1[p44]'.. I'm trying to get the value after the equal sign, but still remain on the same line. So if I were searching by the p45 key, I would get 'u'. If I was searching by the p44 key I would get 1.. Is there a simple regex or something that would allow me to achieve these results? Thank you for any help! I've been looking for a way to do this for a long time. I have a simple(ish) login system, and I want to allow users to login with Twitter or Facebook; How can I do this? I'm pretty experienced at PHP and MySQL, but I really don't understand the API of either of the social networking sites. If you only know how the API for one of the sites, that'd be great too. Hi there, im trying to create a sign up page for my site! I have used confirm password and email fields on the form but the code still shows the error message even if the passwords and emails match! Can anyone advice me please? I am coding using php and I am adding a pound sign in my mysql table but it outputs as a question mark, how do I make it output the pound sign? thanks. I am having an issue with £ that i have in my database showing on my webpage as a triangle ?. I have my database encoded as utf8_general_ci and i declare <meta charset="UTF-8"> in my head. Is there something that i am missing here? Help is appreciated. Kind Regards Adam $username = $_POST['username']; $password = $_POST['password']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $query = mysql_query("INSERT INTO users VALUES ('','$username','$password','$month','$day','$year') mysql_query($query); The code above is a sample of what I have but what I want is to store an entire birthdate in ONE SQL cell. More like this... $username = $_POST['username']; $password = $_POST['password']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $query = mysql_query("INSERT INTO users VALUES ('','$username','$password','$birthdate') mysql_query($query); How is this possible? Can I do this and actually use it efficiently in the future? Hi guys, I've got a snippet of code that's supposed to make a new directory with the user's unique ID (member_id) in the database. I have the code here but I get a myriad of errors including the infamous "at line 1 error, and the directory does not create. Please help me!! //Create Directory $queryString2 = $_SERVER['QUERY_STRING']; $query2 = "SELECT member_id FROM members WHERE $queryString2 = $row[activation]"; $result2 = mysql_query($query2) or die(mysql_error()); mkdir("/home/gafferzo/www/members/clubs/" . mysql_result($result2,0), 0777); Hi. I am brand new here... I have a script where I'm trying to parse an email and everything is working fine but it looks like the percent sign (%) is getting stripped out of my message. The percent sign is in the subject message and it gets removed and I also can't use it in any other strings in the script. I've tried just adding a slash in front like \% but that didn't fix it. Is there another way to escape a percent sign when using it in a script? Any help is appreciated. Thanks. -Jeff The Script:
<?php echo "<h1>The Hashtag: </h1>"; echo $_GET['hashtag']; ?>An example for the URL on localhost: /search.php?hashtag=#xboxIf I am not using the number sign, then the word "xbox" does get printed on screen, if I do use the number sign then the hashtag "#xbox" does not get printed on screen. Any suggestions on how to do this? this code is to create a new user based on posted variables. it works fine for creating the necessary tables and granting privileges. but then when i log out as root and try to log back in as the user it's failing. why is it failing? Thanks, G. Code: [Select] <?php extract( $_POST ); include( '../php/userobject.php' ); @ $db = mysqli_connect('localhost', 'root', 'xxxxxx', 'mealchamp'); if ( mysqli_connect_errno() ) { echo 'Error: could not connect to database as root.<br />'; exit; } else echo 'Connected to database as root.<br />'; $query = "SELECT username FROM userlist WHERE username = $uname"; $result = mysqli_query($db, $query); if ($result) echo 'That user name is already taken.<br/>Please hit back on your browser, and try a different user name.<br/>'; else { echo 'That username is available.<br/>'; echo "$fname<br/>"; echo "$lname<br/>"; echo "$add1<br/>"; echo "$add2<br/>"; echo "$city<br/>"; echo "$prov<br/>"; echo "$ctry<br/>"; echo "$post<br/>"; echo "$email<br/>"; echo "$uname<br/>"; echo "$pass1<br/>"; echo "$sex<br/>"; echo "$birth<br/>"; echo "$height<br/>"; echo "$weight<br/>"; $query = "INSERT INTO userlist (firstname, lastname, address1, address2, city, province, country, postal, email, username, password, sex, birth, height, weight) VALUES ('$fname', '$lname', '$add1', '$add2', '$city','$prov', '$ctry', '$post','$email','$uname', '$pass1', '$sex', '$birth', '$height', '$weight')"; $result = mysqli_query($db, $query); if ($result) echo "Userlist was updated successfully<br />"; else echo "Could not update userlist"; $query = "CREATE TABLE ".$uname."_persfood (persfoodid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, food VARCHAR(30) NOT NULL)"; $result = mysqli_query($db, $query); if ($result) echo "Personal food table was created successfully<br />"; else echo "Could not create personal food table<br />"; $query = "GRANT select ON mealchamp.userlist TO ".$uname." IDENTIFIED BY '$pass1'"; $result = mysqli_query($db, $query); if ($result) echo "User priveleges were granted successfully<br />"; else echo "Could not grant user priveleges<br />"; $query = "GRANT select, insert, update, delete, index ON mealchamp.".$uname."_persfood TO ".$uname." IDENTIFIED BY '$pass1'"; $result = mysqli_query($db, $query); if ($result) echo "User priveleges were granted successfully<br />"; else echo "Could not grant user priveleges<br />"; mysqli_close($db); @ $db = mysqli_connect('localhost', $uname, $pass1, 'mealchamp'); if ( mysqli_connect_errno() ) { echo 'Error: could not connect to database as user.<br />'; exit; } else { echo 'Connected to database as user.<br />'; ?> I am trying to build my own custom login script. What I am trying to achieve is once a user has logged in depending on wether they have checked the keep me logged in checkbox they have two options. If they haven't checked it then it creates session variables only, and if they have checked it it also creates cookie variable as well as the session variables. If they then close their browser / tab without logging out and then revisit the site they will get redirected to login page because the active session variable is no longer there. As soon as they land on the loggin page, it automatically checks for the cookie variable and if it exists, it uses it to login and redirect them automatically. However the problem that I am facing is that the session variable is still being trashed after a default amount of idle time and forcing a login. My goal is that the user shouldn't have to re-login unless they have either clicked the logout button. Can someone please have a look through my solution and advise me as to wether this is the correct method that I am implementing, if there is an easier way to achieve what I want, and is this a secure way to handle user logins. Thanks in advance. Andrew Here is the check code I have placed at the top of each admin page. Code: [Select] <?php session_start(); $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; $uid = $_SESSION['uid']; if (!isset($uid)) { header('location:login.php?redirect='.$url); exit(); } ?> Next we have the code for the login.php file. Code: [Select] <?php include ('functions.php'); ?> <?php get_header('login'); ?> <div id="login-result"> <?php connect(); $redirect = htmlspecialchars(mysql_real_escape_string(addslashes($_GET['redirect']))); if(isset($_COOKIE['remembered'])){ $username = htmlspecialchars(mysql_real_escape_string(addslashes($_COOKIE['remembered']['username']))); $password = htmlspecialchars(mysql_real_escape_string(addslashes($_COOKIE['remembered']['password']))); $sql = "SELECT * FROM usersT WHERE username='$username' AND password='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); $row = mysql_fetch_array($result); $uid = $row['uid']; $fname = $row['firstname']; $lname = $row['lastname']; $role = $row['role']; if($count==1){ $sql2 = "UPDATE usersT SET status = '1' WHERE uid = '$uid'"; $result2 = mysql_query($sql2); if($result2){ session_register("uid"); session_register("uname"); session_register("ulevel"); $_SESSION["uid"] = $uid; $_SESSION["uname"] = $fname; $_SESSION["ufullname"] = $fname . " " .$lname; $_SESSION["urole"] = $role; $home = get_option('home'); if(!empty($redirect)) { header( 'Location: '. $redirect ) ; exit(); } else { header( $home ) ; exit(); } } } else { echo "<div class=\"error rounded5 shadow\">Invalid username or password!</div>"; } } else if (isset($_POST['admin_login'])){ if(isset($_POST["username"]) && isset($_POST["password"])){ $username_p = htmlspecialchars(mysql_real_escape_string(addslashes($_POST["username"]))); $password_p = htmlspecialchars(mysql_real_escape_string(addslashes($_POST["password"]))); $psw = md5($password_p); $sql3 = "SELECT * FROM usersT WHERE username='$username_p' AND password='$psw'"; $result3 = mysql_query($sql3); $count3 = mysql_num_rows($result3); $row3 = mysql_fetch_array($result3); $uid = $row3['uid']; $fname = $row3['firstname']; $lname = $row3['lastname']; $role = $row3['role']; if($count3==1){ $sql4 = "UPDATE usersT SET status = '1' WHERE uid = '$uid'"; $result4 = mysql_query($sql4); if($result4){ session_register("uid"); session_register("uname"); session_register("ulevel"); $_SESSION["uid"] = $uid; $_SESSION["uname"] = $fname; $_SESSION["ufullname"] = $fname . " " .$lname; $_SESSION["urole"] = $role; $home = get_option('home'); if(isset($_POST['remember'])) { setcookie("remembered[username]", $username, time() + 86400 * 365 * 2); setcookie("remembered[password]", $psw, time() + 86400 * 365 * 2); } if(!empty($redirect)) { header( 'Location: '. $redirect ) ; exit(); } else { header( $home ) ; exit(); } } } else { echo "<div class=\"error rounded5 shadow\">Invalid username or password!</div>"; } } } ?> </div><!-- / login-results --> <div id="login" class="rounded5 shadow"> <form name="loginform" id="loginform" action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <p> <label for="username">Username<br> <input type="text" name="username" id="username" class="rounded5" value="<?php echo $username_p; ?>" size="20" tabindex="10" /></label> </p> <p> <label for="password">Password<br> <input type="password" name="password" id="password" class="rounded5" value="<?php echo $password_p; ?>" size="20" tabindex="20" /></label> </p> <p class="submit"> Keep me logged in<input type="checkbox" name="remember" id="remember" /><br /><br /><a href="" class="left">Lost your password?</a> <input type="submit" name="admin_login" id="admin_login" class="btn rounded10 right" value="Log In" tabindex="100" /> </p> <div class="cleaner"></div><!-- / cleaner --> </form> </div><!-- / login--> <?php get_footer('login'); ?> Finally here is the code I am using for the logout.php page. Code: [Select] <?php session_start(); include ('functions.php'); connect(); $uid = mysql_real_escape_string($_SESSION['uid']); $sql = "UPDATE usersT SET status = '0' WHERE uid = '$uid'"; $result = mysql_query($sql); if($result) { session_unset(); session_destroy(); if(isset($_COOKIE['remembered'])){ setcookie("remembered[username]", $username, time() - 3600); setcookie("remembered[password]", $psw, time() - 3600); header("location: login.php"); } exit(); } else { echo "You couldn't be logged out at this time."; } ?> Please help me out with sessions . I m new to this topic.
|