Jump to content

PHP Fatal error: Uncaught Error: Object of class DateTime could not be converted to string


Recommended Posts

Hello ,

I have a made a PHP website where users signup and send their Date of Birth and gets an OTP on their email after signup.

The OTP is recieved but when we enter the OTP this problem occurs 

Quote

Error! Something went wrong 

and I am facing this error in the error log

Quote

PHP Fatal error: Uncaught Error: Object of class DateTime could not be converted to string 

This is my config.php code

<?php
    session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Confirmation Page - Kanha Stories</title>

    <style>
        body
        {
            background-color: #330000;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 800 400'%3E%3Cdefs%3E%3CradialGradient id='a' cx='396' cy='281' r='514' gradientUnits='userSpaceOnUse'%3E%3Cstop offset='0' stop-color='%23D18'/%3E%3Cstop offset='1' stop-color='%23330000'/%3E%3C/radialGradient%3E%3ClinearGradient id='b' gradientUnits='userSpaceOnUse' x1='400' y1='148' x2='400' y2='333'%3E%3Cstop offset='0' stop-color='%23FA3' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23FA3' stop-opacity='0.5'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect fill='url(%23a)' width='800' height='400'/%3E%3Cg fill-opacity='0.4'%3E%3Ccircle fill='url(%23b)' cx='267.5' cy='61' r='300'/%3E%3Ccircle fill='url(%23b)' cx='532.5' cy='61' r='300'/%3E%3Ccircle fill='url(%23b)' cx='400' cy='30' r='300'/%3E%3C/g%3E%3C/svg%3E");
            background-attachment: fixed;
            background-size: cover;
            padding: 20px;
            width: 100vw;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            color:#ffffff;
            overflow-x: hidden;
        }
        .cont
        {
            padding: 20px 40px;
            position: relative;
            border-right: 4px solid rgb(184, 182, 182);
            border-bottom: 4px solid rgb(184, 182, 182);
            border-radius: 15px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        #left{
            float: left;
        }
        #right{
            float: right;
        }
        input{
            margin: 10px 0px;
        }
        s{
            padding: 5px;
        }
        .error{
            padding: 5px;
            color: #ffffff;
        }
        .resend{
            color: rgb(14, 14, 196);
            padding: 5px;
        }
        .s:hover{
            cursor: pointer;
            background-color:gray;
            color: rgb(243, 237, 237);
            border-radius: 5px;
        }
    </style>

</head>
<body>

    <?php
        $code="";
        $err="";
        $error="";
        if(($_SERVER["REQUEST_METHOD"]=="GET" && $_SESSION['xyz'] === 'xyz') || isset($_POST['verify']) || isset($_POST['resend']))
        {
            unset($_SESSION["xyz"]);
            if($_SERVER["REQUEST_METHOD"] ==="POST")
            {
                if(isset($_POST['verify']))
                {
                    if(empty($_POST['code']))
                    {
                        $err="Enter the code!";
                    }
                    else
                    {
                        $code=$_POST['code'];
                        if(password_verify($code,$_SESSION['code']))
                        {
                            $name=$_SESSION['name'];
                            $email=$_SESSION['email'];
                            $tel=$_SESSION['tel'];
                            $dob=$_SESSION['dob'];
                            $password=$_SESSION['password'];
                            $age_category=$_SESSION['age_category'];
                            
                            require_once('./all_utils/connection.php');
                            $sql="INSERT INTO identity_table(name,email,password,tel,dob,age_category) VALUES ('$name','$email','".$password."','$tel','$dob','$age_category')";
                            if(mysqli_query($conn,$sql) === TRUE) 
                            {
                                unset($_SESSION["name"]);
                                unset($_SESSION["password"]);
                                unset($_SESSION["dob"]);
                                unset($_SESSION["tel"]);
                                unset($_SESSION["age_category"]);
                                
                                header("location:welcome/welcome.php");
                            }
                            else
                            {
                                $err="Error! Something went wrong";
                            }
                        }
                        else
                        {
                            $err="Incorrect code!";
                        }

                    }
                }
                elseif(isset($_POST['resend']))
                {
                    require_once('./all_utils/mail.php');
                    $error="OTP has been sent again!";
                }
            }
        }
        else{
            header("location:signup.php");
        }
    ?>

    <div class="cont">
        <h2> Email Verification</h2>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
            <label for="verification">Enter the 5 digit code</label>
            <br/>
            <p> Didn't got the mail?  Please check your spam folder </p>
            <input type="text" name="code" placeholder="Eg. 12345" value="<?php echo $code; ?>">
            <br/>
            <div class="error"><?php echo $err; ?></div>
            <div class="resend"><?php echo $error;?></div>
            <input type="submit" name="resend" class="s" id="left" value="Resend OTP">
            <input type="submit" name="verify" class="s" id="right" value="Verify">
        </form>
    </div>
</body>
</html>

 

This is my signup.php code

<?php
    session_start();
    if(!empty($_SESSION['email']))
    {
        require_once('./all_utils/connection.php');
        $query="SELECT * FROM identity_table WHERE email='".$_SESSION['email']."'";
        $result=mysqli_query($conn,$query);
        if(mysqli_fetch_assoc($result))
        {
            header("location:welcome/welcome.php");
        }
    }

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SignUp - Kanha Stories</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            width: 100vw;
            height: 100vh;
            display: flex;
            align-items: center;
            background-color: #ff9d00;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg stroke='%23000' stroke-width='66.7' stroke-opacity='0' %3E%3Ccircle fill='%23ff9d00' cx='0' cy='0' r='1800'/%3E%3Ccircle fill='%23f27d00' cx='0' cy='0' r='1700'/%3E%3Ccircle fill='%23e55f00' cx='0' cy='0' r='1600'/%3E%3Ccircle fill='%23d84400' cx='0' cy='0' r='1500'/%3E%3Ccircle fill='%23cb2c00' cx='0' cy='0' r='1400'/%3E%3Ccircle fill='%23bf1600' cx='0' cy='0' r='1300'/%3E%3Ccircle fill='%23b20300' cx='0' cy='0' r='1200'/%3E%3Ccircle fill='%23a5000e' cx='0' cy='0' r='1100'/%3E%3Ccircle fill='%2398001c' cx='0' cy='0' r='1000'/%3E%3Ccircle fill='%238b0027' cx='0' cy='0' r='900'/%3E%3Ccircle fill='%237e0030' cx='0' cy='0' r='800'/%3E%3Ccircle fill='%23710037' cx='0' cy='0' r='700'/%3E%3Ccircle fill='%2364003b' cx='0' cy='0' r='600'/%3E%3Ccircle fill='%2358003c' cx='0' cy='0' r='500'/%3E%3Ccircle fill='%234b003a' cx='0' cy='0' r='400'/%3E%3Ccircle fill='%233e0037' cx='0' cy='0' r='300'/%3E%3Ccircle fill='%23310030' cx='0' cy='0' r='200'/%3E%3Ccircle fill='%23210024' cx='0' cy='0' r='100'/%3E%3C/g%3E%3C/svg%3E");
            background-attachment: fixed;
            background-size: cover;
            overflow-x: hidden;
        }

        .cont {
            color: #ffffff;
            width: 500px;
            margin: auto;
        }

        h2 {
            color: #ffffff;
            text-align: center;
            padding: 1.5px;
        }

        .error {
            text-align: center;
            padding: 20px;
            font-size: 1rem;
            color: rgb(233, 76, 76);
        }

        form {
            font-size: 1.2rem;
            /* width: 40%; */
            /* margin: auto; */
        }
        .in{
            margin: 5px 0;
        }

        input {
            border: 2px solid white;
            padding: 10px;
            margin: 5px 0;
            font-size: 1rem;
            width: 100%;
        }

        input:hover {
            border: 2px solid rgb(228, 81, 81);
            cursor: text;
        }

        p,a{
            text-align: center;
            font-size: 1rem;
        }
        a{
            color: deepskyblue;
            font-size:20px;
        }

        #s{
            text-decoration:none;
            border-radius: 12px;
        }
        #s:hover {
            cursor: pointer;
        }

        a {
            text-decoration: none;
        }
        
        @media only screen and (max-width: 600px){
            .cont{
                width: 300px;
            }
            .error,input{
                font-size: 0.8rem;
            }

        }
        @media only screen and (max-width: 400px){
            .cont{
                width: 70%;
            }
            h2{
                font-size: 1.3rem;
            }
            a,p{
                font-size: 0.7rem;
            }
            label{
                font-size: 1.0rem;
            }
            input{
                padding: 4px;
            }

        }
    </style>
</head>
<body>

    <?php

        $name="";        
        $email="";
        $tel="";
        $dob="";
        $err="";
        
        $name_err="";
        $email_err="";
        $tel_err="";
        $dob_err="";
        $password_err="";
    if($_SERVER["REQUEST_METHOD"]=="POST")
       {
            if(isset($_POST['signup']))
            {
                if(empty($_POST['name']) || empty($_POST['dob']) || empty($_POST['tel']) || empty($_POST['email']) || empty($_POST['password']))
                {
                    if(empty($_POST['name']))
                    {
                        $name_err="Name is required!";
                    }
                    else{
                        $name=$_POST['name'];
                    }
                    if(empty($_POST['email']))
                    {
                        $email_err="Email is required!";
                    }
                    else{
                        $email=$_POST['email'];
                    }
                    if(empty($_POST['tel']))
                    {
                        $tel_err="Contact Number is required!";
                    }
                    else{
                        $tel=$_POST['tel'];
                    }
                    if(empty($_POST['dob']))
                    {
                        $dob_err="D.O.B is required!";
                    }
                    else{
                        $dob=$_POST['dob'];
                    }
                    if(empty($_POST['password']))
                    {
                        $password_err="Password is required!";
                    }
                }
                else
                {
                    $today = new DateTime(date('m.d.y'));
                    $dob = new DateTime($_POST['dob']);
                    $diff1 = $today->diff($dob);
                    $age = $diff1->y;
                    if($age > 15 || $age <3)
                    {
                        $dob = $_POST['dob'];
                        $dob_err = "Age criteria not satisfied , child's age must be between 3-15 years";
                    }
                    else
                    {
                        require_once("./all_utils/connection.php");
                        $email=$_POST['email'];
                        $query="SELECT * FROM identity_table WHERE email='".$email."'";
                        $result=mysqli_query($conn,$query);

                        if(mysqli_fetch_assoc($result))
                        {
                            $err="Email alredy registered!";
                            $name=$_POST['name'];
                            $email=$_POST['email'];
                        }
                        else
                        {
                            if($age < 7)
                            {
                                $_SESSION['age_category'] = '1';
                            }
                            else
                            {
                                $_SESSION['age_category'] = '2';
                            }
                            $_SESSION['name']=$_POST['name'];
                            $_SESSION['email']=$_POST['email'];
                            $_SESSION['password'] = password_hash($_POST['password'],PASSWORD_DEFAULT);
                            $_SESSION['tel']=$_POST['tel'];
                            $_SESSION['dob']=$_POST['dob'];
                            $_SESSION['xyz']='xyz';
                            require_once("all_utils/mail.php");
                            header("location:conf.php");
                        }
                    }
                }
            
            }
        }

    ?>

    <div class="cont">
        <h2>SignUp - Kanha Stories</h2>

            <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="POST">

            <span class="error"><?php echo $err; ?></span>
            <br/>

            <label for="name">Name</label><br />
            <input type="text" name="name" placeholder="Enter your name" value="<?php echo $name; ?>">

            <span class="error"><?php echo $name_err; ?></span>
            <br/>

            <label for="email">Email</label><br />
            <input type="email" name="email" placeholder="Enter your Email ID" value="<?php echo $email; ?>">


            <span class="error"><?php echo $email_err;?></span>
            <br/>

            <label for="tel">Mobile Number</label><br />
            <input type="tel" name="tel" placeholder="Enter Mobile Number" value="<?php echo $tel; ?>">


            <span class="error"><?php echo $tel_err;?></span>
            <br/>

            <label for="date">D.O.B. of Child &nbsp; (Click on Calander icon)</label><br />
            
            <input  type="date" name="dob" placeholder="Enter date of birth " value="<?php echo $dob;?>">

            <span class="error"><?php echo $dob_err;?></span>
            <br/>

            <label for="password">Password</label><br />
            <input type="password" name="password" placeholder="Enter your Password">


            <span class="error"><?php echo $password_err; ?></span>
            <br/>
            

            <div class="sub">
            <input type="submit" name="signup" id="s"  placeholder="Submit"><br />
            </div>
        </form>
        <p>Already registered? <a href="./login.php">log in here</a></p>
    </div>
</body>
</html>

 

I don't know what I can do now , Please try to give me the solutions as soon as possible. Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.