Johan Doe
Johan Doe

Reputation: 43

PHP: Serial Number Authentication

I am not very experienced in php and I am trying to learn some basics. Here I have an array which contains serial numbers. I would like it when the input matches a serial number, the serialnumber is removed from the array.

I believe this code accomplishes it. But my issue is setting it up.

I want it when someone visits my site.

http://example.com/serialnumbervalidate.php?key=somekey

that somekey is sent as data to the php script then validated. How can this be accomplished?

<?php

    $serialnumbers= array('2r3ewfasd', '21rfsasad', '34t3rfdsas');
    foreach ($serialnumbers as $number) {
        if(($key = array_search($number, $serialnumbers )) !== false) {
            unset($messages[$number]);
            die("found"); 
        } else {  
            die("not found.");
        }  
    }           
?>

Upvotes: 0

Views: 573

Answers (3)

Professor Abronsius
Professor Abronsius

Reputation: 33813

To remove the key as denoted in the querystring you could do the following

$userkey=$_GET['key'];


$serialnumbers= array('2r3ewfasd', '21rfsasad', '34t3rfdsas');
if( in_array( $userkey, $serialnumbers ) ){
    array_splice( $serialnumbers, array_search( $userkey, $serialnumbers ), 1 );
}

I'm not sure why you think the found value is not being removed from the array. Consider the following test

/* utility function */
function pre($data){
    echo '<pre>',print_r($data,true),'</pre>';
}


$serialnumbers= array('2r3ewfasd', '21rfsasad', '34t3rfdsas');

/* Show original data in array */
pre($serialnumbers);

/* set a variable with user supplied `key` parameter */
$userkey=$_GET['key'];

/* search for and remove `$key` from original array */
if( in_array( $userkey, $serialnumbers ) ){
    array_splice( $serialnumbers, array_search( $userkey, $serialnumbers ), 1 );
}   

/* Show array after operation */
pre($serialnumbers);

/* pseudo test url used */
#https://localhost/test/?key=34t3rfdsas

/* output */
Array
(
    [0] => 2r3ewfasd
    [1] => 21rfsasad
    [2] => 34t3rfdsas
)
Array
(
    [0] => 2r3ewfasd
    [1] => 21rfsasad
)

Upvotes: 4

symcbean
symcbean

Reputation: 48367

Rather than explain why your code isn't working, consider:

$serialnumbers= array('2r3ewfasd', '21rfsasad', '34t3rfdsas');
foreach ($serialnumbers as $index=>$number) {
    if($_GET['key'] === $number) {
        unset($messages[$index]);
        die("found"); 
    } else {  
        die("not found.");
    }  
}           

But it would be less effort (and when you have more than 3 items in the array, a lot faster) to do this:

$serialnumbers= array(
 '2r3ewfasd'=>1, 
 '21rfsasad'=>1, 
 '34t3rfdsas'=>1);
if ($serialnumbers[$_GET['key']]) {
  $serialnumbers[$_GET['key']]=0;
  die ('found');
}
die ('not found');

Upvotes: 1

Esteban
Esteban

Reputation: 1815

The array_search function already loop through your array, so don't do it yourself, as for your question, $_GET[<name of your variable>] shoud be used :

<?php
$number = $_GET['key'];
$serialnumbers= array('2r3ewfasd', '21rfsasad', '34t3rfdsas');
if(($key = array_search($number, $serialnumbers )) !== false) {
    unset($messages[$number]);
    die("found"); 
}else{  
    die("not found.");
}
?>

Upvotes: 1

Related Questions