0

So I am working on a code signing system for iOS. I need a user's UDID before they can access the website. How can I pass the javascript prompt input to a php variable.

I have tried posting the variable back to the same page.

<?php
    $udid = $_POST['udid'];
    if(empty($udid)){
        $udid = file_get_contents("saves/" . $ip . ".txt");
    }
    if(empty($udid)){
?>
<script>
var udid=prompt("Please enter your UDID");
$.ajax(
{
    type: "POST",
    url: "app.php",
    data: udid,
    success: function(data, textStatus, jqXHR)
    {
        console.log(data);
    }
});
</script>
<?php
    }
if( strpos(file_get_contents("cert1.txt"),$udid) !== false) {
            echo "Device status:<br><span class='badge badge-dark'>Signed</span><br>";
            echo "Signed on cert:<br><span class='badge badge-dark'>1</span><br>";
        } else {
            $t = ' ' . time();
            echo "<p>Device status:<br><span class='badge badge-dark'>Unsigned</span><br>You are now<br>on the waitlist</p><script>alert(\"Your device isn't approved yet. We have added you to the waitlist. Check back soon.\");</script>";
            $txt = $_GET['udid'] . $t;
            $myfile = file_put_contents('notsigned.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
            header("Location: notsigned.php");
        }
?>
<br>
<a href="http://get.udid.io" style="text-decoration:none;font-family:arial;font-size:15px;color:#fff;padding:8px;border-radius:5px;background-color:blue;margin-bottom:5px;">Get your udid</a>
<br><br>
<form class='form-horizontal well' action='#' method='post'>
    <input type='text' name='udid' class='input-large' size="9" border="2" placeholder="udid" value='<?= $udid ?>'>
    <button type="submit" id="submit" style="text-decoration:none;font-family:arial;font-size:15px;color:#fff;padding:8px;border-radius:5px;background-color:springgreen;margin-bottom:5px;" class="badge-primary">Save</button>
</form>
<?php
    setcookie("udid", $udid, time()+31536000000, "/");
file_put_contents("saves/" . $ip . ".txt",$udid);
if(empty($udid)){
    alert('You cannot access anything till you enter your udid.');
}
?>

What I need it to do is set $udid (PHP) to what the user entered in either the prompt or the input form.

6
  • 2
    What are you asking? You have some Ajax, which is one way of passing this. What is the problem? Commented Apr 23, 2019 at 11:55
  • The php isn't receiving the variable. Commented Apr 23, 2019 at 11:56
  • 1
    Look in the console. Also I would expect and ELSE in there to not continue if the UDID is not passed. And why not store it in a session var? Commented Apr 23, 2019 at 11:56
  • 1
    you should have data: {udid: udid} rather than data: udid, I think? Otherwise how does your PHP backend know that the key it's looking for is "udid"? [Note this can be abbreviated as just data: {udid} if you're using ES6.] Commented Apr 23, 2019 at 11:58
  • Mind explaining what a session var is? I will check the console. Also what do you mean by you would expect the else to not continue Commented Apr 23, 2019 at 11:59

2 Answers 2

1

reposting my comment as an answer (with a little more detail):

You should have data: {udid: udid} rather than data: udid. The documentation says that data should be on "object, string or array", but it only mentions a string in the explicit case that it's a query string (eg ?key1=value1&key2=value2). By passing it as an object as shown then you ensure that the PHP backend will be able to access $_POST['udid'] and it will have the intended value.

Note: this object can be abbreviated as just data: {udid} if you're using ES6.

Sign up to request clarification or add additional context in comments.

1 Comment

Done. Deleted mine. Thanks again
1

Change the data attribute to the following,

data: {
    udid: udid
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.