Jump to content

AndrewM16921

Members
  • Posts

    9
  • Joined

  • Last visited

Everything posted by AndrewM16921

  1. I just wanted to create a sort of "active in last 15 min" list of my users. Seemed kind of weird to use a db for that. I'll have to look into this apc thing. o.O Thanks
  2. So I was wondering if there is a way to create a server-like behavior with php. That is to say, a sort of "static" variable set that can be accessed and shared between all php sessions. And if this is possible, how would I go about setting it up? Thanks
  3. I'm trying to write a little password encryption tool (mostly as a learning experience - but I may end up using it too ). And the encryption and decryption functions I wrote seem to work just fine (see crypt_test.php below). But, for some reason when I try to decrypt a result from a MySql database it doesn't seem to work (see db.php below). Not sure if it's some sort of weird data type issue? Or something else entirely. Code is below... maybe somebody can point me in the right direction. :/ Edit: Seems the encryption function creates a whole different string each execution, which is why the test worked but the db stuff doesn't. Guessing it has something to do with MCRYPT_RAND... help still appreciated though. Thanks ahead of time. crypt.php <?php function encrypt($s) { $key = pack('H*', "-"); //took out keys for forum post $key_size = strlen($key); $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $s_utf8 = utf8_encode($s); $cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $s_utf8, MCRYPT_MODE_CBC, $iv); $cipher = $iv . $cipher; $cipher_base64 = base64_encode($cipher); return $cipher_base64; } function decrypt($s) { $key = pack('H*', "-"); //took out keys for forum post $key_size = strlen($key); $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $cipher_dec = base64_decode($s); $iv_dec = substr($cipher_dec, 0, $iv_size); $cipher_dec = substr($cipher_dec, $iv_size); $decipher_utf8 = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $cipher_dec, MCRYPT_MODE_CBC, $iv_dec); $decipher_utf8 = str_replace("0", "", $decipher_utf8); return $decipher_utf8; }?> crypt_test.php <?php include('crypt.php'); $p = "password"; $c = encrypt($p); $d = decrypt($c); echo $p.'<br />'; echo $c.'<br />'; echo $d.'<br />'; //works just fine?> db.php <?php include_once('crypt.php'); function connect() { $con = mysql_connect('-', '-', '-'); if(!$con) die('Could not connect: ' . mysql_error()); mysql_select_db('-', $con); } function login($user, $pass) { connect(); $user = mysql_real_escape_string($user); $pass = mysql_real_escape_string($pass); $sql = "SELECT uid, password FROM Accounts WHERE username='$user'"; $result = mysql_query($sql); if($row = mysql_fetch_array($result)) { echo $pass . " : "; echo $row['password'] . " : "; echo decrypt($row['password']); //garbled nonsense if($pass == decrypt($row['password'])) return $row['uid']; } return -1; } function register($user, $pass, $email) { connect(); $user = mysql_real_escape_string($user); $pass = mysql_real_escape_string($pass); $pass = encrypt($pass); $email = mysql_real_escape_string($email); $sql = "INSERT INTO Accounts (username, password, email) VALUES ('$user', '$pass', '$email')"; mysql_query($sql); }?>
  4. AndrewM16921

    json to php

    Hey, So I'm trying to teach myself how to use json with php, made a simple example. I have a script on the browser side var data = JSON.stringify({'username':user,'password':pass});$.ajax({ url: 'login.php', type: 'POST', data: data, contentType: 'application/json; charset=utf-8', dataType: 'json', success: function(result) { alert(result); }}); So, in login.php how do I access the data I sent? Is it in the $_POST array? And if so... what's mapped to it? I know there's the json_decode/encode functions, but my problem is finding the object to use it on and I have no idea. O_o Help appreciated. Thanks.
  5. Actually, I just fixed the problem. Stupid mistake! Instead of if(isset($_SESSION["uid"])) I forgot to negate it if(!isset($_SESSION["uid"])) But thank you!
  6. Hey so, I'm trying to get a php session to work with ajax calls from jquery. It doesn't seem to be working, so I'm guessing something like a session id isn't getting sent by the ajax calls. Not sure how to fix this. Googled some stuff, wasn't entirely sure what to make of it all. I'll post some examples I whipped up to test it. Pretty simplistic example. The js below gets "info.php", which should return a couple spans, the first of which (for testing sake) should just increment each time it's called. $(function(){ $("#sp-info").load("info.php");} <?php session_start(); if(isset($_SESSION["uid"])) { $_SESSION["uid"] = 1; } else { $_SESSION["uid"] = $_SESSION["uid"] + 1; }?><span id="info-left"> Welcome, <? echo $_SESSION["uid"]; ?>!</span>
×
×
  • Create New...